我在這裏遇到了另一個R問題。我有以下佈局的超過2000的觀察和50列的數據幀:R根據組/子網格對數據幀進行彙總
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 2 = subgroup column2 = obs1,column4 = sum obs2之和....
應保留組,子組和列標籤。 我試過了:
for(i in levels(e$group)){test[i,]<-as.matrix(Matrix::colSums(e[,3:4]))}
但是這會導致錯誤。可能你注意到我沒有使用R編碼進行升級:)
http://stackoverflow.com/questions/17447120/subtracting-a-specific-condition-for-each-measure今天早些時候非常相關的問給你。只需用'sum'替換函數即可。 – intra
你只是在尋找'aggregate(cbind(obs1,obs2)〜group + subgroup,e,sum)'或許? – A5C1D2H2I1M1N2O1R2T1
@Ananda Mahto:這幾乎是我在找的東西,但是我怎麼告訴R使用特定的觀察範圍,例如e [,3:4]。我嘗試過'all.obs <-colnames(e [,3:4])',然後使用這個向量而不是cbind(obs1,obs2),但它不起作用。 – user2386786