What is it for?
The relocate()
function is used to change the order of columns in a dataframe. This can be done in a few different ways.
Arguments are the data frame, followed by the column(s) to be moved, then the new position.
Move one column
If a new position is not specified, the default is to move the column to the first position. This can be used to move one or more columns. Use .before or .after to designate the new column position.
penguins %>%
head() %>% gt()
species |
island |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(year, bill_depth_mm) %>%
head() %>% gt()
year |
bill_depth_mm |
species |
island |
bill_length_mm |
flipper_length_mm |
body_mass_g |
sex |
2007 |
18.7 |
Adelie |
Torgersen |
39.1 |
181 |
3750 |
male |
2007 |
17.4 |
Adelie |
Torgersen |
39.5 |
186 |
3800 |
female |
2007 |
18.0 |
Adelie |
Torgersen |
40.3 |
195 |
3250 |
female |
2007 |
NA |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
2007 |
19.3 |
Adelie |
Torgersen |
36.7 |
193 |
3450 |
female |
2007 |
20.6 |
Adelie |
Torgersen |
39.3 |
190 |
3650 |
male |
penguins %>%
relocate(year, .after = island) %>%
head() %>% gt()
species |
island |
year |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
Adelie |
Torgersen |
2007 |
39.1 |
18.7 |
181 |
3750 |
male |
Adelie |
Torgersen |
2007 |
39.5 |
17.4 |
186 |
3800 |
female |
Adelie |
Torgersen |
2007 |
40.3 |
18.0 |
195 |
3250 |
female |
Adelie |
Torgersen |
2007 |
NA |
NA |
NA |
NA |
NA |
Adelie |
Torgersen |
2007 |
36.7 |
19.3 |
193 |
3450 |
female |
Adelie |
Torgersen |
2007 |
39.3 |
20.6 |
190 |
3650 |
male |
# or
relocate(penguins, year, .after = island) %>%
head() %>% gt()
species |
island |
year |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
Adelie |
Torgersen |
2007 |
39.1 |
18.7 |
181 |
3750 |
male |
Adelie |
Torgersen |
2007 |
39.5 |
17.4 |
186 |
3800 |
female |
Adelie |
Torgersen |
2007 |
40.3 |
18.0 |
195 |
3250 |
female |
Adelie |
Torgersen |
2007 |
NA |
NA |
NA |
NA |
NA |
Adelie |
Torgersen |
2007 |
36.7 |
19.3 |
193 |
3450 |
female |
Adelie |
Torgersen |
2007 |
39.3 |
20.6 |
190 |
3650 |
male |
Move a block of columns
Similar to functions like select(), a block of columns can be chosen.
penguins %>%
head() %>% gt()
species |
island |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(body_mass_g:year, .before = bill_length_mm) %>%
head() %>% gt()
species |
island |
body_mass_g |
sex |
year |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
Adelie |
Torgersen |
3750 |
male |
2007 |
39.1 |
18.7 |
181 |
Adelie |
Torgersen |
3800 |
female |
2007 |
39.5 |
17.4 |
186 |
Adelie |
Torgersen |
3250 |
female |
2007 |
40.3 |
18.0 |
195 |
Adelie |
Torgersen |
NA |
NA |
2007 |
NA |
NA |
NA |
Adelie |
Torgersen |
3450 |
female |
2007 |
36.7 |
19.3 |
193 |
Adelie |
Torgersen |
3650 |
male |
2007 |
39.3 |
20.6 |
190 |
Move using tidyselect
helpers
penguins %>%
head() %>% gt()
species |
island |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(contains("_mm"), .after = species) %>%
head() %>% gt()
species |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
island |
body_mass_g |
sex |
year |
Adelie |
39.1 |
18.7 |
181 |
Torgersen |
3750 |
male |
2007 |
Adelie |
39.5 |
17.4 |
186 |
Torgersen |
3800 |
female |
2007 |
Adelie |
40.3 |
18.0 |
195 |
Torgersen |
3250 |
female |
2007 |
Adelie |
NA |
NA |
NA |
Torgersen |
NA |
NA |
2007 |
Adelie |
36.7 |
19.3 |
193 |
Torgersen |
3450 |
female |
2007 |
Adelie |
39.3 |
20.6 |
190 |
Torgersen |
3650 |
male |
2007 |
Move columns based on their type
Another handy option is to rearrange the columns based on the type of data. The penguins
dataset contains both factors and numbers.
penguins %>%
head() %>% gt()
species |
island |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(where(is.numeric), .after = where(is.factor)) %>%
head() %>% gt()
species |
island |
sex |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
year |
Adelie |
Torgersen |
male |
39.1 |
18.7 |
181 |
3750 |
2007 |
Adelie |
Torgersen |
female |
39.5 |
17.4 |
186 |
3800 |
2007 |
Adelie |
Torgersen |
female |
40.3 |
18.0 |
195 |
3250 |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
female |
36.7 |
19.3 |
193 |
3450 |
2007 |
Adelie |
Torgersen |
male |
39.3 |
20.6 |
190 |
3650 |
2007 |
Bonus: Rename
An additional feature of this function is the ability to rename columns. Again, the default is to move the renamed column to the first position in the dataframe. This is easily addressed by specifying the position with .before or .after. This is a nice option if you want to move a column and rename it all at once.
penguins %>%
head() %>% gt()
species |
island |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(site = island) %>%
head() %>% gt()
site |
species |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Torgersen |
Adelie |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Torgersen |
Adelie |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Torgersen |
Adelie |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Torgersen |
Adelie |
NA |
NA |
NA |
NA |
NA |
2007 |
Torgersen |
Adelie |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Torgersen |
Adelie |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
penguins %>%
relocate(site = island, .after = species) %>%
head() %>% gt()
species |
site |
bill_length_mm |
bill_depth_mm |
flipper_length_mm |
body_mass_g |
sex |
year |
Adelie |
Torgersen |
39.1 |
18.7 |
181 |
3750 |
male |
2007 |
Adelie |
Torgersen |
39.5 |
17.4 |
186 |
3800 |
female |
2007 |
Adelie |
Torgersen |
40.3 |
18.0 |
195 |
3250 |
female |
2007 |
Adelie |
Torgersen |
NA |
NA |
NA |
NA |
NA |
2007 |
Adelie |
Torgersen |
36.7 |
19.3 |
193 |
3450 |
female |
2007 |
Adelie |
Torgersen |
39.3 |
20.6 |
190 |
3650 |
male |
2007 |
Is it helpful?
This is a very useful function, especially with large dataframes with many columns. It is also very useful when new columns are added and need to be placed in specific positions. relocate()
allows you to easily rearrange columns to better display the data for presentation and analysis.