Reorder Columns of DataFrame in R

How to reorder (change the order) columns of DataFrame in R? There are several ways to rearrange or reorder columns in R DataFrame for example sorting by ascending, descending, rearranging manually by index/position or by name, only changing the order of first or last few columns, randomly changing only one specific column, replacing one specific column with another and many more.

1. Quick Examples of Reordering Columns

Following are quick examples of reordering or changing the order of the columns in DataFrame.


# Reorder column by index manually
df2 <- df[, c(5, 1, 4, 2, 3)]

# Reorder column by name manually
new_order = c("emp_id","name","superior_emp_id","dept_id","dept_branch_id")
df2 <- df[, new_order]

# Sort columns in ascending alphabetical order
new_order = sort(colnames(df))
df2 <- df[, new_order]

# Sort columns in descending
new_order = sort(colnames(df),decreasing=TRUE)
df2 <- df[, new_order]

# Reorder only first few columns
df2 <- df[, c(2, 1,3:5)]

# If you are not sure length of columns
df2 <- df[,c(2,1,3:ncol(df))]

# Using subset() & select()
df2 <- subset(df, select=c(5, 1, 4, 2, 3))

# Using dplyr
df2 = df %>% select(emp_id,name,superior_emp_id,dept_id,dept_branch_id)

# By range of columns
df2 = df %>% select(emp_id:name)

# Using everything()
df2 <- df %>% select(dept_id, dept_branch_id, everything())

#Reorder random column by name
df2 <- df %>% relocate(emp_id, .before = name)

First, let’s create a DataFrame from an R vector.


# Create Data Frame
df=data.frame(
  name=c("Smith","Rose"),
  dept_id=c(10,20),
  dept_branch_id= c(101,102),
  superior_emp_id=c(-1,1),
  emp_id=c(1,2)
)
df

Yields below output.

2. Reorder Columns by Index in R

If you have a small dataframe with manageable columns and you wanted to reorder the columns in a specific order you can manually create a vector of an index with the order you wanted and use this with df[], the following example changes the order of the dataframe columns by index.


# Reorder column by index manually
df2 <- df[, c(5, 1, 4, 2, 3)]
df2

Yields below output.

change order columns index

3. Reorder Columns by Name in R

Similarly, you can also reorder the columns by name, this approach is also useful if you have a few columns and you wanted to change the order in a specific way.


# Reorder column by name manually
new_order = c("emp_id","name","superior_emp_id","dept_id","dept_branch_id")
df2 <- df[, new_order]
df2

Yields below output.

r reorder columns name

4. Sort Column Names By Ascending (alphabetical) Order

If you don’t have a specific order in mind and wanted to reorder the R dataframe columns by sorting all column names in ascending (alphabetical) order use the sort() function on top of colnames(). Here, colnames() returns all column names from the dataframe as vector and sort() function sorts the vector, and the result of sort use it on df[] to select the columns.


# Sort columns in ascending alphabetical order
new_order = sort(colnames(df))
df2 <- df[, new_order]
df2

Yields below output.

r reorder columns ascending

5. Sort Column Names By Descending Order

Similarly to sort column names by descending order in R, use the decreasing=TRUE parameter on the sort() function. Here, colnames() returns all column names from the dataframe as vector and sort() function sorts the vector in descending order, and the result of sort use it on df[] to select the columns.


# Sort columns in descending
new_order = sort(colnames(df),decreasing=TRUE)
df2 <- df[, new_order]
df2

Yields below output.

r reorder dataframe columns

4. Reorder only First Few Columns in R

If you have many columns on the dataframe, it is not easy to type all column names into a list when you wanted to reorder either the first few or the last few columns in R. You can achieve this by changing the order of the first few columns and select the rest by range.


# Reorder only first few columns
df2 <- df[, c(2, 1,3:ncol(df))]
df2

Yields below output.

5. Using subset() and select()

You can also use subset() and select() to change the order of the columns.


# Using subset() & select()
df2 <- subset(df, select=c(5, 1, 4, 2, 3))
df2

Yields below output.

change order dataframe columns

6. Recorder Columns using dplyr Package in R

Use select() function from dplyr package to reorder or change the order of columns in R, to use select() function, you have to install dplyr first using install.packages(‘dplyr’) and load it using library(dplyr).

All functions in dplyr package take data.frame as a first argument. When we use dplyr package, we mostly use the infix operator %>% from magrittr, it passes the left-hand side of the operator to the first argument of the right-hand side of the operator. For example, x %>% f(y) converted into f(x, y) so the result from the left-hand side is then “piped” into the right-hand side. 

6.1 dplyr select()

This example uses the dplyr select() function to change the order of dataframe columns by name.


# Using dplyr
df2 = df %>% select(emp_id,name,superior_emp_id,dept_id,dept_branch_id)
df2

6.2 Arrange Columns in Reverse Order

You can also arrange the columns in reverse order.


# By range of columns
df2 = df %>% select(emp_id:name)
df2

6.3 Using everything()

To rearrange the only first few columns use the select() with everything(), this everything() function levels the rest of the columns as-is.


# Using everything()
df2 <- df %>% select(dept_id, dept_branch_id, everything())
df2

6.4 Reorder a Specific Column

If you wanted to reorder a specific single column without touching other columns in R, you can achieve this by using relocate() function.


#Reorder random column by name
df2 <- df %>%
  relocate(emp_id, .before = name)
df2

Conclusion

In this article, you have learned how to reorder columns of the DataFrame in R by using several ways. For example change the order of the dataframe columns by sorting in ascending or descending order, by index, by name, by a range of column names, by changing a single column name, and many more R examples.

Related Articles

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Reorder Columns of DataFrame in R