How to reorder Data Frame columns by column names and index in R (with examples)

Renesh Bedre    2 minute read

In this article, you will learn how to reorder Data Frame columns by column names and indexes. To reorder Data Frame columns, we will Data Frame index, subset(), and dplyr select() and relocate() functions.

First, create a sample Data Frame

df <- data.frame(col1 = c("A", "B", "C"),
  col2 = c(1, 2, 3),
  col3 = c(0.1, 0.2, 0.3),
  col4 = c("abc", "def", "xyz"))

df 
# output
  col1 col2 col3 col4
1    A    1  0.1  abc
2    B    2  0.2  def
3    C    3  0.3  xyz

Reorder columns by column indexes

The Data Frame df contains four columns (col1, col2, col3, col4) and these column names need to be reordered as (col3, col1, col2, col4).

You will use column indexes to reorder columns,

df <- df[, c(3, 1, 2, 4)]

df 
# output
  col3 col1 col2 col4
1  0.1    A    1  abc
2  0.2    B    2  def
3  0.3    C    3  xyz

You can also use subset function to reorder columns by indexes,

subset(df, select=c(3, 1, 2, 4))
# output
  col3 col1 col2 col4
1  0.1    A    1  abc
2  0.2    B    2  def
3  0.3    C    3  xyz

Reorder columns by column names

Here, You will use column names to reorder columns,

df <- df[, c("col3", "col1", "col2", "col4")]

df 
# output
  col3 col1 col2 col4
1  0.1    A    1  abc
2  0.2    B    2  def
3  0.3    C    3  xyz

You can also use dplyr select() function to reorder column by column names,

library(dplyr)
df %>% select(col3, col1, col2, col4)
# output
  col3 col1 col2 col4
1  0.1    A    1  abc
2  0.2    B    2  def
3  0.3    C    3  xyz

Reorder the column names in alphabetical order,

library(dplyr)
df %>% select(order(colnames(df)))
# output
 col1 col2 col3 col4
1    A    1  0.1  abc
2    B    2  0.2  def
3    C    3  0.3  xyz

Reorder the column names in descending alphabetical order,

library(dplyr)
df %>% select(order(colnames(df), decreasing = T))
# output
  col4 col3 col2 col1
1  abc  0.1    1    A
2  def  0.2    2    B
3  xyz  0.3    3    C

Reorder columns using dplyr relocate()

You can also use dplyr relocate() function to reorder the columns of a Data Frame. relocate() works with Data Frame, tibble, or lazy Data Frame.

Move the last column to the first position,

library(dplyr)
df %>% relocate(col4)
# output
 col4 col1 col2 col3
1  abc    A    1  0.1
2  def    B    2  0.2
3  xyz    C    3  0.3

Move col4 after col1,

df %>% relocate(col4, .after = col1)
# output
  col1 col4 col2 col3
1    A  abc    1  0.1
2    B  def    2  0.2
3    C  xyz    3  0.3

Move col4 before col3,

df %>% relocate(col4, .before = col3)
# output
 col1 col2 col4 col3
1    A    1  abc  0.1
2    B    2  def  0.2
3    C    3  xyz  0.3

Move col1 after last column in a Data Frame,

df %>% relocate(col1, .after = last_col())
# output
  col2 col3 col4 col1
1    1  0.1  abc    A
2    2  0.2  def    B
3    3  0.3  xyz    C




Enhance your skills with courses on R

If you have any questions, comments or recommendations, please email me at reneshbe@gmail.com


This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.

Tags:

Updated: