Home » How to Use the relocate() Function in dplyr (With Examples)

How to Use the relocate() Function in dplyr (With Examples)

by Erma Khan

You can use the relocate() function from the dplyr package in R to change the column positions in a data frame.

You can use the following methods to change the column positions:

Method 1: Move One Column to Front

#move 'x' column to front
df %>% relocate(x)

Method 2: Move Several Columns to Front

#move 'x' and 'y' columns to front
df %>% relocate(x, y)

Method 3: Move Column to Position After Another Column

#move 'x' column to position after 'y' column
df %>% relocate(x, .after=y)

Method 4: Move Column to Position Before Another Column

#move 'x' column to position before 'y' column
df %>% relocate(x, .before=y)

The following examples show how to each method with the following data frame:

#create dataset
df frame(team=c('A', 'A', 'A', 'B', 'B', 'C', 'C'),
                 points=c(1, 2, 3, 4, 5, 6, 7),
                 assists=c(1, 5, 2, 3, 2, 2, 0),
                 rebounds=c(6, 6, 10, 12, 8, 8, 3))

#view dataset
df

  team points assists rebounds
1    A      1       1        6
2    A      2       5        6
3    A      3       2       10
4    B      4       3       12
5    B      5       2        8
6    C      6       2        8
7    C      7       0        3

Example 1: Move One Column to Front

The following code shows how to use the relocate() function to move one column to the front:

#move 'assists' column to front
df %>% relocate(assists)

  assists team points rebounds
1       1    A      1        6
2       5    A      2        6
3       2    A      3       10
4       3    B      4       12
5       2    B      5        8
6       2    C      6        8
7       0    C      7        3

Example 2: Move Several Columns to Front

The following code shows how to use the relocate() function to move multiple columns to the front:

#move 'points' and 'assists' to front
df %>% relocate(points, assists)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Example 3: Move Column to Position After Another Column

The following code shows how to use the relocate() function to move one column to a specific position after another column:

#move 'team' column to after 'assists' column
df %>% relocate(team, .after=assists)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Example 4: Move Column to Position Before Another Column

The following code shows how to use the relocate() function to move one column to a specific position before another column:

#move 'team' column to before 'rebounds' column
df %>% relocate(team, .before=rebounds)

  points assists team rebounds
1      1       1    A        6
2      2       5    A        6
3      3       2    A       10
4      4       3    B       12
5      5       2    B        8
6      6       2    C        8
7      7       0    C        3

Additional Resources

The following tutorials explain how to perform other common functions using dplyr:

How to Remove Rows Using dplyr
How to Arrange Rows Using dplyr
How to Filter by Multiple Conditions Using dplyr

Related Posts