如何将聚合与列名列表一起使用

时间:2013-08-29 22:13:15

标签: r aggregate

如何通过传递条件和值列表来汇总函数中的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)

我已经回顾了许多替代方案,但还没有发现这种抽象的关键。

2 个答案:

答案 0 :(得分:9)

您可以使用aggregate的替代界面,该界面不使用公式:

c1 <- c("cond1","cond2","cond3")
v1 <- c("value1","value2")
aggregate(x[v1],by=x[c1],FUN=sum)

   cond1 cond2 cond3     value1      value2
1      a     A     A -3.3025839 -0.98304649
2      b     A     A  0.6326985 -3.08677485
3      c     A     A  3.6007853  2.23962265
4      a     B     A -0.5247620 -0.94644740
5      b     B     A  0.9242562  2.48268452
6      c     B     A  6.9215712  0.31512645

答案 1 :(得分:1)

c1 <- list( c("cond1","cond2","cond3"), c("cond2","cond3"),c("cond3"))
v1 <- list( c("value1","value2"),c("value2"),c("value1"))

 mapply(FUN= function(z,y, ...) {aggregate(x[ , y], by=x[z], ...)},
           c1, v1, MoreArgs=list(FUN=sum) )

结果是三个数据帧的列表

[[1]]
   cond1 cond2 cond3      value1      value2
1      a     A     A  0.19396539  1.11536490
2      b     A     A -1.20056699 -5.36713982
3      c     A     A -0.19716521 -2.06737461
4      a     B     A  1.58880450 -7.62452134
5      b     B     A -4.68579210  0.47266047
6      c     B     A  2.70550795 -0.50020883
7      a     C     A  1.69312219 -4.26851536
8      b     C     A  0.99236424  4.85013434
snipped remaining 76 rows

[[2]]
   cond2 cond3           x
1      A     A -6.31914953
2      B     A -7.65206970
3      C     A  1.36818527
4      D     A  3.77492482
5      E     A  2.68977303
snipped 23 rows

[[3]]
  cond3          x
1     A   8.104481
2     B  17.766659
3     C -14.577315
4     D   4.398249