Reordering Data Frame Columns in R

Previously, we described the essentials of R programming and provided quick start guides for importing data into R as well as converting your data into a tibble data format, which is the best and modern way to work with your data. We also described crutial steps to reshape your data with R for easier analyses.


Here, you we’ll learn how to reorder columns, in your data table, by either column positions or column names.


Reordering Data Table Columns in R

Pleleminary tasks

  1. Launch RStudio as described here: Running RStudio and setting up your working directory

  2. Prepare your data as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files

  3. Import your data into R as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in iris data set, which we start by converting to a tibble data frame (tbl_df). Tibble is a modern rethinking of data frame providing a nicer printing method. This is useful when working with large data sets.

# Create my_data
my_data <- iris
# Convert to a tibble
library("tibble")
my_data <- as_data_frame(my_data)
# Print
my_data
Source: local data frame [150 x 5]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
                                  
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
..          ...         ...          ...         ...     ...

Reorder column by position

# Get column names
colnames(my_data)
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

my_data contains 5 columns ordered as follow:

  1. Sepal.Length
  2. Sepal.Width
  3. Petal.Length
  4. Petal.Width
  5. Species

But we want:

  • the variable “Species” to be the first column (1)
  • the variable “Petal.Width” to be the second column (2)

It’s possible to reorder the column by position as follow:

my_data2 <- my_data[, c(5, 4, 1, 2, 3)]
my_data2
Source: local data frame [150 x 5]
   Species Petal.Width Sepal.Length Sepal.Width Petal.Length
                                  
1   setosa         0.2          5.1         3.5          1.4
2   setosa         0.2          4.9         3.0          1.4
3   setosa         0.2          4.7         3.2          1.3
4   setosa         0.2          4.6         3.1          1.5
5   setosa         0.2          5.0         3.6          1.4
6   setosa         0.4          5.4         3.9          1.7
7   setosa         0.3          4.6         3.4          1.4
8   setosa         0.2          5.0         3.4          1.5
9   setosa         0.2          4.4         2.9          1.4
10  setosa         0.1          4.9         3.1          1.5
..     ...         ...          ...         ...          ...

Reorder column by name

col_order <- c("Species", "Petal.Width", "Sepal.Length",
               "Sepal.Width", "Petal.Length")
my_data2 <- my_data[, col_order]
my_data2
Source: local data frame [150 x 5]
   Species Petal.Width Sepal.Length Sepal.Width Petal.Length
                                  
1   setosa         0.2          5.1         3.5          1.4
2   setosa         0.2          4.9         3.0          1.4
3   setosa         0.2          4.7         3.2          1.3
4   setosa         0.2          4.6         3.1          1.5
5   setosa         0.2          5.0         3.6          1.4
6   setosa         0.4          5.4         3.9          1.7
7   setosa         0.3          4.6         3.4          1.4
8   setosa         0.2          5.0         3.4          1.5
9   setosa         0.2          4.4         2.9          1.4
10  setosa         0.1          4.9         3.1          1.5
..     ...         ...          ...         ...          ...

Summary


It’s possible to reorder columns by either column position (i.e., number) or column names.


References

Infos

This analysis has been performed using R (ver. 3.2.3).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen 511645 times