I've created a dynamic column name w/ dplyr::mutate() based on this thread Use dynamic variable names in `dplyr` and now I want to sort the new column.... but I'm not correctly passing the column name
library(glue)
library(dplyr)
# data
set.seed(123)
df <- data.frame(distance = sample(1:100, size = 10))
# custom function
multiply_function <- function(df, metric, multiplier){
df %>%
mutate(., "{{metric}}_x{{multiplier}}" := {{metric}} * multiplier) %>%
arrange(desc("{{metric}}_x{{multiplier}}")) # <--- this is not working
}
df %>%
multiply_function(., metric = distance, multiplier = 3)
distance distance_x3
1 31 93
2 79 237
3 51 153
4 14 42
5 67 201
6 42 126
7 50 150
8 43 129
9 97 291
10 25 75