[R] blockwise sums

Vicente Canto Casasola vicented.canto.ext at juntadeandalucia.es
Wed Sep 1 13:03:31 CEST 2004


Hi, all!!

 From the help page for 'aggregate':
     Splits the data into subsets, computes summary statistics for
     each, and returns the result in a convenient form.

So here's the solution I found to this problem:

blocksums <- function(x,n)
{
temp <- 1:length(x)-1
temp <- list((temp%/%n)+1)
aggregate(x,by=temp,sum)$x
}

For instance:
blocksums(1:10,3)
[1]  6 15 24 10

Hope this helps!!
Vicente.




More information about the R-help mailing list