[R] apply a function to a rolling subset of a vector

Gabor Grothendieck ggrothendieck at myway.com
Wed Mar 2 23:47:32 CET 2005


Whit Armstrong <whit <at> twinfieldscapital.com> writes:

: 
: Does anyone know an easy way to calculate the rolling 20 period average
: or sum of a vector?
: 
: For instance:
: x <- rnorm(1000)
: 
: y <- apply.subset(x,20,fun="sum")
: 
: The first element of y would contain the sum of elements 1 to 20, the
: second element of y 
: would contain the sum of elements 2:21, and so on.
: 
: I thought I had seen this on the list a year or so ago, but I couldn't
: find anything in the archives.
: 

Look at ?filter .  Also ?embed and gtools::running .  filter is the
fastest.




More information about the R-help mailing list