Reorder columns of a data frame in R is quite important when you want to deal with large data set and have large columns. Reordering columns of a data frame is helpful to keep important and useful columns at the beginning for data analysis.

Using the select() function in R and relocate() function available in the dplyr package of tidverse helps to change the column position of a data frame in R.

In this tutorial, we will discuss how to reorder the columns of a data frame in R using the select(), and relocate functions.

Reorder Columns of Data Frame in R using select()

Using the select() function in base R, we can change the column order of a data frame in R.

Let’s practice with an example to change the position of columns in a data frame.

Change the column order of data frame by column name

Create a data frame using data.frame() function. It has three columns id, name, and revenue.

# Create a data frame
sales_data <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Ebook","Audio","Video","Course","Book","Software"),
  revenue = c(45500,53000,55200,13650,33400,13800)
)
# Print the data frame
sales_data

Run the above R code to create a data frame and print it.

The output of the above R code is:

  id     name revenue
1  1    Ebook   45500
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

To change the column order of a data frame using the select() function, use the following code.

# Change the order of column, move second column
select(sales_data,name,id,revenue)

In the above R code, using the select function, it changes the position of the second column to the first position.

In the output below, refer name column move to the first position.

      name id revenue
1    Ebook  1   45500
2    Audio  2   53000
3    Video  3   55200
4   Course  4   13650
5     Book  5   33400
6 Software  6   13800

Change the column order of data frame by column index

Using the select() function in R, we can change the column position of the data frame by column index.

Use the above sales_data for illustration purposes.

Change the position of the third column and first column. Move the third column to the first position and the first column to the third position in a data frame.

# Change column position in data frame by index
select(sales_data,3,1,2)

The output of the above R code is:

 revenue id     name
1   45500  1    Ebook
2   53000  2    Audio
3   55200  3    Video
4   13650  4   Course
5   33400  5     Book
6   13800  6 Software

Reorder columns of data frame using select

If you want to move the column to the first position and keep all other columns as is, use the following code.

sales_data %>% select(revenue,everything())

In the below output, the revenue column moves to the first position.

  revenue id     name
1   45500  1    Ebook
2   53000  2    Audio
3   55200  3    Video
4   13650  4   Course
5   33400  5     Book
6   13800  6 Software

Rearrange the column names of data frame in alphabetical order

Using the select(), colnames(), and order () function in base R, we can rearrange the column name of a data frame.

Use the above sales_data frame for illustration purposes.

# Rearrange the column name in alphabetical order
sales_data %>% select (order(colnames(sales_data)))

The output of the above R code reorders the column name by alphabetical order like id, name, and revenue.

 id     name revenue
1  1    Ebook   45500
2  2    Audio   53000
3  3    Video   55200
4  4   Course   13650
5  5     Book   33400
6  6 Software   13800

Reorder the column names of data frame in reverse alphabetical order

Using the select(), colnames(), and order () function in base R, we can rearrange the column name of a data frame in alphabetical descending order.

Use the above sales_data frame for illustration purposes.

# Reorder the column name of data frame in reverse alphabetical order
sales_data %>% select (order(colnames(sales_data),decreasing = TRUE))

In the above R code, we have used the select function to select the columns, and using the order() function, it reordered the column name.

decreasing = TRUE parameter change the position of the columns in the data frame in reverse alphabetical order.

 revenue     name id
1   45500    Ebook  1
2   53000    Audio  2
3   55200    Video  3
4   13650   Course  4
5   33400     Book  5
6   13800 Software  6

Reorder Columns of Data Frame using relocate

Use the relocate() function of the dplyr package to move the columns of a data frame. It works similarly to the select() function.

Use the above sales_data data frame to understand relocate() function to move the column of a data frame.

# Change the column order using the relocate and after
sales_data %>% relocate(revenue,.after = id)

In the above R code, relocate() function reorder the column of a data frame to move after the column id. The new column order of the data frame should be id, revenue, and name.

The output of the above R code is:

  id revenue     name
1  1   45500    Ebook
2  2   53000    Audio
3  3   55200    Video
4  4   13650   Course
5  5   33400     Book
6  6   13800 Software

Let’s reorder the column of the data frame using relocate() function to move before the specified column. The new column order of a data frame should be name, id, and revenue.

# Move the column before the specified column using relocate() of tidyverse
sales_data %>% relocate(name,.before =id )

The output of the above R code is:

      name id revenue
1    Ebook  1   45500
2    Audio  2   53000
3    Video  3   55200
4   Course  4   13650
5     Book  5   33400
6 Software  6   13800

Conclusion

I hope the above article on how to reorder columns of a data frame in R is helpful to you.

Using the select() function and relocate() function of the dplyr package, we can change the column position in a data frame.