Relative Relocating

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:

  • Relative to Beginning: Move one or more columns to the beginning (the left of the data frame) while leaving the rest of the columns where they are.
  • Relative to Column: Move one or more columns to positions specified relative to, i.e. before or after, a particular column.
  • Relative to Position: Move one or more columns to positions specified relative to, i.e. before or after, a particular column position.

Relative to Beginning

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:

  • We use the function relocate() from dplyr to move one or more columns to new positions.
  • To move one or more columns to the front of a data frame, we simply pass to relocate() the names of those column(s), which here are col_3 and col_4.
  • The output data frame 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:

  • The workhorse of selecting columns in the tidyverse is the select() operation.
  • The issue with using 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.
  • The output here is the same as the primary solution above.

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:

  • We use select(last_col():1) to invert the order of columns of a data frame.
  • We can use the column operator : with select() to select a range of columns. See Basic Selection.
  • The helper last_col() of select() allows us to refer to the last column.
  • Since we specify the range as last_col():1, select() selects columns starting at the last column and moving in reverse order of columns to the first column.
  • The output data frame 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…

Relative to Column

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:

  • We use the function relocate() to move one or more columns to be before a given column.
  • We pass to relocate() the column(s) that we wish to move, which here are col_1 and col_4.
  • We pass to the .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.

Relative to Position

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:

  • We use relocate() to move columns to a certain position. See Relative to Column above.
  • To specify a position relative to the end, we use the last_col() helper which points to the last column of the data frame.
  • We use the offset argument of 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.
R
I/O