0

我被另一个 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=sum of obs1, column4=sum obs2 ....

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

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

但这会导致错误。可能你注意到我对 R 编码并不先进 :)

4

1 回答 1

0

从您的评论来看,您可能想要查看“data.table”包,它可以快速处理此类聚合,并且语法非常简单。

在这种情况下,您可以尝试以下操作:

library(data.table)
E <- data.table(e)
E[, lapply(.SD, sum), by = c("group", "subgroup"), .SDcols = 3:4]
于 2013-07-03T16:34:03.953 回答