r-功能问题.Tidyval过滤



这里出了什么问题?这项工作:

iris %>% 
filter(Species == "setosa") %>% 
summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))

并生产:

msl   msw
1 5.006 0.246

但这个功能不起作用:

means <- function(data, value){
data <- enquo(data)
value <- enquo(value)
data %>% 
filter(Species == !!value) %>% 
summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))
}

并且CCD_ 1产生这个错误:

UseMethod中的错误("filter_"(:没有适用于"filter_"的方法应用于类"c('qusure','formula'("的对象从:filter_(.data,.dots=compat_as_lazy_dots(…((调用

错误消息非常简单,您无法过滤qu肯定。我不知道你为什么要查询你的数据,但这会让你得到你想要的:

means <- function(data, value){
value <- enquo(value)
data %>% 
filter(Species == !!value) %>% 
summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))
}

最新更新