We wish to relocate one or more columns relative to the current column locations while keeping all other column locations relatively unchanged.
We will cover the following scenarios:
We wish to move one or more columns to the beginning (the left of the data frame) while leaving the rest of the columns where they are.
In this example, we wish to move columns col_3
and col_4
of the data frame df
to be the leftmost columns.
df_2 = df %>%
relocate(col_3, col_4)
Here is how this works:
relocate()
from dplyr
to move one or more columns to new positions.relocate()
the names of those column(s), which here are col_3
and col_4
.df_2
will be a copy of the input data frame df
with columns col_3
and col_4
moved to the first and second leftmost positions respectively.Alternative: via Selecting
df_2 = df %>%
select(col_3, col_4, everything())
Here is how this works:
tidyverse
is the select()
operation.select()
on its own to relocate one or more columns to the front is that we have to spell out the names of all columns, and risk missing some. Thanks to the tidyverse
developers though, select()
has a helper everything()
which returns all columns that are not mentioned by name.Extension: Relocate and Rename
df_2 = df %>%
relocate(col_c = col_3, col_d = col_4)
Here is how this works:
The function relocate()
allows us to rename columns while relocating them making data pipelines less verbose.
Extension: Reverse Order
We wish to inverse the order of columns of a data frame; i.e. the last column becomes the first column and the second last column becomes the second column, etc…
df_2 = df %>%
select(last_col():1)
Here is how this works:
select(last_col():1)
to invert the order of columns of a data frame.:
with select()
to select a range of columns. See Basic Selection.last_col()
of select()
allows us to refer to the last column.last_col():1
, select()
selects columns starting at the last column and moving in reverse order of columns to the first column.df_2
will be a copy of the input data frame df
with columns in the reverse order; i.e. the last column becomes the first column and the second last column becomes the second column, etc…We wish to move one or more columns to positions specified relative to, i.e. before or after, a particular column.
In this example, we wish to move columns col_1
and col_4
of the data frame df
to be before the column col_3
.
df_2 = df %>%
relocate(col_1, col_4, .before = col_3)
Here is how this works:
relocate()
to move one or more columns to be before a given column.relocate()
the column(s) that we wish to move, which here are col_1
and col_4
..before
argument of relocate()
the name of the column that we wish to move the columns relative to, which here is .before = col_3
.Extension: After
df_2 = df %>%
relocate(col_1, col_2, .after = col_3)
Here is how this works:
This works similarly to the primary solution above except that we use the argument .after
to specify the name of the column that we wish to move the selected set of column relative to.
We wish to move one or more columns to positions specified relative to, i.e. before or after, a particular column position.
In this example, we wish to move columns col_1
and col_4
of the data frame df
to be before position 2 (third column) relative to the current column order of the data frame.
df_2 = df %>%
relocate(col_3, col_4, .before = 2)
Here is how this works:
This works similarly to the solution presented under Relative to Column above except that we pass to the argument .before
of relocate()
the position of the column we wish to move the selected columns relative to (rather than its name).
Extension: After
df_2 = df %>%
relocate(col_1, col_2, .after = 3)
Here is how this works:
This works similarly to the solution presented under Relative to Column above except that we pass to the argument .after
of relocate()
the position of the column we wish to move the selected columns relative to (rather than its name).
Extension: Relative to End
In this example, we wish to move columns col_1
and col_4
of the data frame df
to be before the second last column from the end.
df_2 = df %>%
relocate(col_1, col_4, .before = last_col(offset = 2))
Here is how this works:
relocate()
to move columns to a certain position. See Relative to Column above.last_col()
helper which points to the last column of the data frame.last_col()
to refer to columns relative to the end of the data frame, which we set here to offset = 2
to specify that we wish to move columns col_1
, and col_4
to be before the second last column from the end.