如何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)
Run Code Online (Sandbox Code Playgroud)
需要创建列名列表:(显示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)
Run Code Online (Sandbox Code Playgroud)
我已经回顾了许多替代方案,但还没有发现这种抽象的关键.