我被另一个 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 编码并不先进 :)