你如何aggregate
通过传递一个条件和值的列表来总结一个函数?
# This works fine:
x <- data.frame(cond1 = sample(letters[1:3], 500, replace=TRUE),
cond2 = sample(LETTERS[1:7], 500, replace = TRUE),
cond3 = sample(LETTERS[1:4], 500, replace = TRUE),
value1 = rnorm(500),
value2 = rnorm(500))
aggregate(cbind(value1,value2) ~ cond1 + cond2, data = x, FUN=sum)
需要创建列名列表:(显示 3 个选项)然后调用函数:
c1 <- c("cond1","cond2","cond3"); v1 <- c("value1","value2")
c1 <- c("cond2","cond3"); v1 <- c("value2")
c1 <- c("cond3"); v1 <- c("value1")
aggregate(cbind(v1) ~ c1, data = x, FUN=sum)
我已经审查了许多替代方案,但还没有发现这种抽象的关键。