R根据组/子组汇总数据帧

时间:2013-07-03 15:45:35

标签: r sum dataframe aggregate

我被另一个R问题困在这里。我有一个以下布局的数据框,有超过2000个观察值和50列:

group <- c("X","X","X","X","Y","Y","Y","Y","Z","Z","Z","Z")
subgroup <- c("A","B","A","B","A","B","A","B","A","B","A","B")
obs1 <- c(rnorm(12,mean=10))
obs2 <- c(rnorm(12,mean=20))
e <- data.frame(group,subgroup,obs1,obs2)

我想要的是一个数据框,显示特定组x子组交互的所有观察值的总和,如下所示: column1 = group column2 = subgroup column2 = obs1,column4 = sum obs2 ....

的总和

应保留组,子组和列标签。 我试过了:

for(i in levels(e$group)){test[i,]<-as.matrix(Matrix::colSums(e[,3:4]))}

但这会导致错误。可能你注意到我没有使用R编码进步:)

1 个答案:

答案 0 :(得分:0)

根据您的评论判断,您可能希望查看“data.table”包,它可以快速处理此类聚合并使用非常简单的语法。

在这种情况下,您可以尝试以下内容:

library(data.table)
E <- data.table(e)
E[, lapply(.SD, sum), by = c("group", "subgroup"), .SDcols = 3:4]