We wish to identify the columns to use for sorting, not by explicitly spelling out their names but by specifying criteria satisfied by the desired columns.
In this example we wish to sort the rows of the data frame df
by all the columns whose names start with the string prefix cvr_
. Note: We are assuming that the order of the columns is appropriate for the task at hand.
df_2 = df %>%
arrange(across(starts_with('cvr_')))
Here is how this works:
across()
allows us to apply an operation on multiple columns. Its most common use is in data transformation which we cover in Implicit Transformation but is also the function to use in the current implicit sorting context.starts_with()
to select all columns whose names start with the string suffix ‘cvr_’
. See Implicit Selection for a coverage of the most common scenarios of implicit column selection including by name pattern, data type, and Criteria satisfied by the column’s data.arrange(across(..))
arrange(across(starts_with('cvr_')))
arrange(across(where(is.character)))