这里是一个解决方案以及我是如何得出它的。
group_by 期望什么?
> group_by
function (x, ..., add = FALSE)
{
new_groups <- named_dots(...)
进入兔子洞:
> dplyr:::named_dots
function (...)
{
auto_name(dots(...))
}
<environment: namespace:dplyr>
> dplyr:::auto_name
function (x)
{
names(x) <- auto_names(x)
x
}
<environment: namespace:dplyr>
> dplyr:::auto_names
function (x)
{
nms <- names2(x)
missing <- nms == ""
if (all(!missing))
return(nms)
deparse2 <- function(x) paste(deparse(x, 500L), collapse = "")
defaults <- vapply(x[missing], deparse2, character(1), USE.NAMES = FALSE)
nms[missing] <- defaults
nms
}
<environment: namespace:dplyr>
> dplyr:::names2
function (x)
{
names(x) %||% rep("", length(x))
}
利用这些信息,如何着手制定解决方案?
ChickWeight %>% do.call( group_by, list( Chick, Diet ) ) %>% summarise( mw = mean( weight ) )
do.call( group_by, list( x = ChickWeight, Chick, Diet, add = FALSE ) ) %>% summarise( mw = mean( weight ) )
解决方案在于引用参数,以便推迟它们的评估,直到它们处于包含 x
tbl 的环境中:
do.call( group_by, list( x = ChickWeight, quote(Chick), quote(Diet), add = FALSE ) ) %>% summarise( mw = mean( weight ) )
v <- "Diet"
do.call( group_by, list( x = ChickWeight, quote(Chick), substitute( a, list( a = v ) ), add = FALSE ) ) %>% summarise( mw = mean( weight ) )
group_by_( c( "Chick", v ) )
代替group_by( c( "Chick", v ) )
。 - David Arenburgdplyr
不可行,您也可以轻松地使用data.table
完成 :) ,例如library(data.table) ; as.data.table(ChickWeight)[, .(mw = mean(weight)), c("Chick", v)]
。 - David Arenburgsummarise
,主要是因为没有summarize_each
。这样我就少了一件要记住的事情。 - Rich Scrivensummarize_each
可能是一个疏忽)。 - Konrad Rudolph