有没有办法确保mutate_each_
(或者可能funs_
)在父框架中查找函数?考虑:
library(dplyr) # 0.4.1
library(magrittr) # 1.5
fun_list <- list(a=quote(rev), b=quote(sort))
iris[1:5, 1:2] %>% mutate_each_(funs_(fun_list), c("Sepal.Length"))
按照您的预期工作:
Sepal.Length Sepal.Width a b
1 5.1 3.5 5.0 4.6
2 4.9 3.0 4.6 4.7
3 4.7 3.2 4.7 4.9
4 4.6 3.1 4.9 5.0
5 5.0 3.6 5.1 5.1
可是:
my_rev <- rev
my_srt <- sort
fun_list2 <- list(a=quote(my_rev), b=quote(my_srt))
iris[1:5, 1:2] %>% mutate_each_(funs_(fun_list2), c("Sepal.Length"))
错误:
Error in mutate_impl(.data, dots) : could not find function "my_rev"
简单的mutate
有效:
iris[1:5, 1:2] %>% mutate(a=my_rev(Sepal.Length), b=my_srt(Sepal.Length))
答案 0 :(得分:6)
您应在此处使用公式~
表示法,而不是quote()
my_rev <- rev
my_srt <- sort
fun_list2 <- list(a = ~my_rev, b = ~my_srt)
iris[1:5, 1:2] %>% mutate_each_(funs_(fun_list2), c("Sepal.Length"))
来自非标准评估小组:
最好使用公式,因为公式可以捕获两者 要评估的表达式,以及它应该成为的环境 评估。