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

2005-03-02 Thread Whit Armstrong
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.

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

2005-03-02 Thread Ken Knoblauch
Try this: ?convolve x-rnorm(1000) y-rep(1,20) z-convolve(x,y,type=filter) plot(x,type=l) str(z) num [1:981] 6.31 7.28 8.16 7.39 4.65 ... lines(c(rep(0,10),z,rep(0,10)),col=yellow,lwd=3) lines(c(rep(0,10),z,rep(0,10))/length(y),col=red,lwd=3) #running mean You wrote: Does anyone know an

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

2005-03-02 Thread Duncan Murdoch
On Wed, 2 Mar 2005 17:22:43 -0500, Whit Armstrong [EMAIL PROTECTED] wrote : 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,

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

2005-03-02 Thread Marc Schwartz
On Wed, 2005-03-02 at 17:22 -0500, Whit Armstrong wrote: 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

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

2005-03-02 Thread Gabor Grothendieck
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,

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

2005-03-02 Thread Whit Armstrong
Thanks, everyone, for all the suggestions. The rollFun turs out to be just what I needed. Cheers, Whit -Original Message- From: Kjetil Brinchmann Halvorsen [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 02, 2005 5:45 PM To: Whit Armstrong Cc: r-help@stat.math.ethz.ch Subject: Re: