I can calculate the rank of the values (val) in my dataframe df within the group name1 with the code:
res <- df %>% arrange(val) %>% group_by(name1) %>% mutate(RANK=row_number())
Instead of writing the column "name1" in the code, I want to pass it as variable, eg crit = "name1". However, the code below does not work since crit1 is assumed to be the column name instead of a variable name.
res <- df %>% arrange(val) %>% group_by(crit1) %>% mutate(RANK=row_number())
How can I pass crit1 in the code?
Thanks, Tom
We can use
group_by_
Update
group_by_
is deprecated in the recent versions (now usingdplyr
version -0.8.1
), so we can usegroup_by_at
which takes a vector of strings as input variablesOr another option is to convert to symbols (
syms
fromrlang
) and evaluate (!!!
)data