RE: [R] apply a function to a rolling subset of a vector
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: [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@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. __ R-help@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
Re: [R] apply a function to a rolling subset of a vector
Whit Armstrong 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. __ R-help@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
Re: [R] apply a function to a rolling subset of a vector
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 > 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. You can use the running() function in the gtools package, which is in the gregmisc bundle: x <- rnorm(1000) > running(x, fun = sum, width = 20) 1:20 2:21 3:22 4:23 5:24 -2.009684610 -2.205737077 -1.410810606 -2.226661837 -1.684604289 6:25 7:26 8:27 9:28 10:29 -4.492008605 -3.816273719 -5.348364598 -6.444591766 -5.263013812 11:30 12:31 13:32 14:33 15:34 -4.609829115 -5.935537291 -6.909232329 -4.881021777 -5.803659103 ... See ?running for more information, after installing gregmisc from CRAN. HTH, Marc Schwartz __ R-help@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
Re: [R] apply a function to a rolling subset of a vector
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, 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. I don't know of a general purpose function, but filter() (in the stats package) can do the example you give, or any other linear filter. e.g. x <- rnorm(1000) y <- filter(x, rep(1,20)) puts 20 element sums into y. The vector ends up the same length as x, with NAs at the beginning and end (by default). Duncan Murdoch __ R-help@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
[R] apply a function to a rolling subset of a vector
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 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 __ R-help@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
[R] apply a function to a rolling subset of a vector
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]] __ R-help@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