[R] rolling sum (like in Rmetrics package)

Romain Francois rfrancois at mango-solutions.com
Wed Feb 13 17:44:16 CET 2008


Hello,

rollapply in zoo can probably help you :

 > x <- zoo( 1:10 )
 > rollapply( x, 4, sum )
 2  3  4  5  6  7  8
10 14 18 22 26 30 34
 > sum( x[1:4] )
[1] 10
 > sum( x[2:5] )
[1] 14

Cheers,

Romain

-- 
Mango Solutions
data analysis that delivers

Introduction to R training course :: London :: 06-07 March 2008
http://www.mango-solutions.com/services/rtraining/r_intro.html





joshv wrote:
> Hello, I'm new to R and would like to know how to create a vector of "rolling
> sums". (I have seen the Rmetrics package and the rollMean function and I
> would like to do the same thing except Sum instead of Mean.)  I imagine
> someone has done this, I just can't find it anywhere.    
>
> Example:
> x <- somevector   #where x is 'n' entries long
>
> #what I would like to do is:
>
> x1 <- x[1:20]
> output1 <- sum(x1)
>
> x2 <- x[2:21]
> output2 <- sum(x2)
>
> x3 <- ...
>
> ouput <- c(output1, output2, ...)
>
>
> Thanks,
> JV
>



More information about the R-help mailing list