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 meanYou 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 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. Thanks in advance, Whit [[alternative HTML version deleted]] ____________________ Ken Knoblauch Inserm U 371 Cerveau et Vision 18 avenue du Doyen Lepine 69675 Bron cedex France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: 06 84 10 64 10
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. Thanks in advance, Whit [[alternative HTML version deleted]]
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:kjetil at acelerate.com] Sent: Wednesday, March 02, 2005 5:45 PM To: Whit Armstrong Cc: r-help at stat.math.ethz.ch Subject: Re: [R] apply a function to a rolling subset of a vector 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") > >help.search("rolling") gives me (among others) RollingAnalysis(fSeries) Rolling Analysis so trying library(fSeries) x <- rnorm(1000) y <- rollFun(x, 20, mean) Kjetil>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. > > >Thanks in advance, >Whit > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html > > > > >-- Kjetil Halvorsen. Peace is the most effective weapon of mass construction. -- Mahdi Elmandjra -- No virus found in this outgoing message. Checked by AVG Anti-Virus.
Seemingly Similar Threads
- How to compute Rolling analysis of Standard Deviation using ZOO package?
- Survey of "moving window" statistical functions - still looking f or fast mad function
- Multiple comparisons with a glm
- adding a line to a single panel of a lattice plot
- AIC in glm.nb and glm(...family=negative.binomial(.))