Using dcast from the reshape2 library in R I aggregate data (sum)
library('reshape2')
DF <- data.frame(Cohort=rep("", 9), Weeks=rep("", 9), myvalue=rep(0, 9), stringsAsFactors=FALSE )
DF[1, ] <- c("2012_30","0",0.02)
DF[2, ] <- c("2012_30","1",0.01)
DF[3, ] <- c("2012_30","2",0.1)
DF[4, ] <- c("2012_31","0",0.08)
DF[5, ] <- c("2012_31","1",0.0)
DF[6, ] <- c("2012_31","2",0.3)
DF[7, ] <- c("2012_32","0",0.26)
DF[8, ] <- c("2012_32","1",0.01)
DF[9, ] <- c("2012_32","2",0.01)
dcast(DF, Cohort ~ Weeks, value = myvalue, fill='')
This produces
Cohort 0 1 2
2012_30 0.02 0.01 0.10
2012_31 0.08 0.00 0.30
2012_32 0.26 0.01 0.01
...
I would like to be able to get the accumulated value like this:
Cohort 0 1 2
2012_30 0.02 0.03 0.13
2012_31 0.08 0.00 0.38
2012_32 0.26 0.27 0.28
...
I tried to find examples for the fun.aggregate in the dcast documentation from reshape2 and the original reshape paper