这是怎么了?这有效:
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))
}
和means(iris, "setosa")
会产生此错误:
UseMethod(“ filter_”)中的错误: 没有适用于“ filter_”的适用方法应用于类“ c('quosure','formula')”的对象 调用来自:filter _(。data,.dots = compat_as_lazy_dots(...))
答案 0 :(得分:5)
该错误消息非常简单明了,您无法筛选出确定。我不知道您为什么要入队数据,但这将为您提供所需的信息:
means <- function(data, value){
value <- enquo(value)
data %>%
filter(Species == !!value) %>%
summarise(msl = mean(Sepal.Length), msw = mean(Petal.Width))
}