2
这是新的dplyr,即将发布。在dplyr中以编程方式指定的多个变量进行分组0.6
的dplyr programming vignette给出呼叫group_by
与在外部函数中指定的分组变量的一个示例:
my_summarise <- function(df, group_var) {
df %>%
group_by(!!group_var) %>%
summarise(a = mean(a))
}
当与单个分组变量提供这工作。但是,它失败了多个变量。
简单的例子:
f <- function(x)
{
group_by(mtcars, !!x)
}
## works
g1 <- "cyl"
f(g1)
## doesn't work
#Error in mutate_impl(.data, dots) :
# Column `c("cyl", "gear")` must be length 32 (the number of rows) or one, not 2
g2 <- c("cyl", "gear")
f(g2)
我怎么能解决这个问题,rlang框架内?
理想情况下,我希望f
的签名保持不变,即我将分组变量指定为单个向量,而不是通过参数...
。
其实,'F(G1)'不能正常工作。尝试'f(g1)%>%汇总(n = n())'。 – JasonWang