Skip to content
Prev 258229 / 398502 Next

Yearly aggregates and matrices

On Wed, Apr 27, 2011 at 2:03 PM, mathijsdevaan <mathijsdevaan at gmail.com> wrote:
You could test the speed of this to see if its faster:

library(reshape2)
library(zoo)
mm <- melt(DF, id = c("B", "C"))
aa <- acast(mm, C ~ B + variable, FUN = sum)
sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA
r <- rollapply(aa, 3,  sum.na, align = "right", partial = TRUE)