0

I am stuck here with another R-problem. I have a data frame of the following layout with over 2000 observations and 50 columns:

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)

What I would like to have is a dataframe showing the sum of all observations for a specific group x subgroup interaction like this: column1=group column2=subgroup column2=sum of obs1, column4=sum obs2 ....

group,subgroup and column labels should be preserved. I tried:

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

but this leads to an error. Probably you notice that I am not advanced with R coding :)

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
user2386786
  • 725
  • 9
  • 25
  • http://stackoverflow.com/questions/17447120/subtracting-a-specific-condition-for-each-measure asked earlier today and very relevant to you. just replace the function with `sum`. – intra Jul 03 '13 at 15:49
  • Are you just looking for `aggregate(cbind(obs1, obs2) ~ group + subgroup, e, sum)` perhaps? – A5C1D2H2I1M1N2O1R2T1 Jul 03 '13 at 15:49
  • @ Ananda Mahto: This was almost what I was looking for but how do I tell R to use a specific range of observations, like e[,3:4] in this example. I tried `all.obs<-colnames(e[,3:4])` and then using this vector instead of cbind(obs1,obs2) but it did not work. – user2386786 Jul 03 '13 at 16:09

1 Answers1

0

Judging by your comment, you might want to look into the "data.table" package which can handle such aggregation quickly and with pretty straightforward syntax.

In this case you can try something like:

library(data.table)
E <- data.table(e)
E[, lapply(.SD, sum), by = c("group", "subgroup"), .SDcols = 3:4]
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • @user2386786, no problem. There are ways to do this in base R too (for example, with `aggregate`) but I find the "data.table" approach in this case to be the most intuitive. – A5C1D2H2I1M1N2O1R2T1 Jul 03 '13 at 17:49