我已经基于此线程使用 dplyr::mutate() 创建了一个动态列名称在 `dplyr` 中使用动态变量名称,现在我想对新列进行排序....但我没有正确传递该列姓名
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 …Run Code Online (Sandbox Code Playgroud) 我有一个大数据框,其中百分比写为 10% 而不是 0.1。并非所有列都是百分比,但相当多的是。
有没有一种优雅的方法将所有 % 转换为小数?我特别关心百分比可能大于 100% 的情况,并且该规则可以应用于整个 tibble,而不必弄清楚要定位哪些列。
示例如果不清楚......这个:
tibble(cola = c("hello", "good bye", "hi there"), colb = c("10%", "20%", "100%"), colc = c(53, 67, 89),cold = c("10%", "200%", "50%") )
Run Code Online (Sandbox Code Playgroud)
对此:
tibble(cola = c("hello", "good bye", "hi there"), colb = c(.10, .20, 1.0), colc = c(53, 67, 89),cold = c(.10, 2.0, .5) )
Run Code Online (Sandbox Code Playgroud)
谢谢。