We wish to specify the sorting columns dynamically i.e. through a variable or a function argument. In particular, we cover the following scenarios:
In this section we cover the specification of sorting column names dynamically. For more involved dynamic sorting scenarios:
One Column
We wish to pass one sorting column to a function wherein sorting takes place.
m_arrange <- function(.df, var) {
.df %>%
arrange({{ var }})
}
df %>% m_arrange(desc(col_1))
Here is how this works:
df
) and the column to sort by (which is here col_1
){{ }}
operator to "tunnel" a data variable through a function argument.desc()
to sort in descending order like we did here with desc(col_1)
.N Columns
We wish to pass a fixed number of sorting columns to a function wherein sorting takes place.
In this example, we wish to pass two columns to a custom function where sorting takes place.
m_arrange <- function(.df, var_1, var_2) {
.df %>%
arrange({{ var_1 }}, {{ var_2 }})
}
df %>% m_arrange(col_1, desc(col_2))
Here is how this works:
{{ }}
operator to "tunnel" each of the data variables through their respective function argument.desc()
to have it be sorted in descending order.Multiple Columns
We wish to pass an arbitrary number of sorting columns (i.e. the number of sorting columns is not known in advance) to a function wherein sorting takes place.
m_arrange <-function(.data, vars){
.data %>%
arrange(across({{ vars }}))
}
df %>%
m_arrange(c(col_1, col_2, col_3))
Here is how this works:
{{ }}
operator to "tunnel" a list of variables through a function argument to across()
.desc
to the second argument of across to sort in descending order of all columns.cur_column()
in that function to apply desc to the appropriate columns.The names of the column that we wish to sort by are specified as strings in an environment variables (or a function argument).
One Column
col = 'col_1'
df %>%
arrange(across({{ col }}, desc))
Here is how this works:
{{ }}
operator to "tunnel" a string variable. {{ }}
is used when we want a variable to be substituted in place before being evaluated.across()
can take a function as its second argument. To sort in descending order, we can pass desc to the second argument of across()
.Multiple Columns
cols = c('col_1', 'col_2', 'col_3')
df %>%
arrange(across({{ cols }}))
Here is how this works:
{{ }}
operator to "tunnel" a vector of strings stored in a variable (here cols
). {{ }}
is used when we want a variable to be substituted in place before being evaluated.desc
to the second argument of across to sort in descending order of all columns.cur_column()
in that function to apply desc to the appropriate columns.