Is there an implementation of moving window functionality so I can apply any function while sliding trough window. Thanks
rollapply in the zoo package does that. See ?rollapply and the three accompanying vignettes.> library(zoo) > z <- zoo(1:10) > rollapply(z, 3, sum)2 3 4 5 6 7 8 9 6 9 12 15 18 21 24 27 On Sat, Sep 27, 2008 at 10:45 AM, milicic.marko <milicic.marko at gmail.com> wrote:> Is there an implementation of moving window functionality so I can > apply any function while sliding trough window. > > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks Gabor, But the function I intend to apply requires data.frame object ... not zoo() object have you had expirience with this kind of problems. On Sep 27, 3:59?pm, "Gabor Grothendieck" <ggrothendi... at gmail.com> wrote:> rollapply in the zoo package does that. See ?rollapply and the > three accompanying vignettes. > > > library(zoo) > > z <- zoo(1:10) > > rollapply(z, 3, sum) > > ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 > ?6 ?9 12 15 18 21 24 27 > > On Sat, Sep 27, 2008 at 10:45 AM, milicic.marko <milicic.ma... at gmail.com> wrote: > > Is there an implementation of moving window functionality so I can > > apply any function while sliding trough window. > > > Thanks > > > ______________________________________________ > > R-h... at r-project.org mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Try this:> BOD # comes with RTime demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8> rollapply(as.zoo(as.matrix(BOD)), 3, sum)2 6 37.6 3 9 45.3 4 12 50.6 5 16 51.4 rollapply has many more options and capabilities. See ?rollapply On Sat, Sep 27, 2008 at 11:16 AM, milicic.marko <milicic.marko at gmail.com> wrote:> Thanks Gabor, > > But the function I intend to apply requires data.frame object ... not > zoo() object > > have you had expirience with this kind of problems. > > > On Sep 27, 3:59 pm, "Gabor Grothendieck" <ggrothendi... at gmail.com> > wrote: >> rollapply in the zoo package does that. See ?rollapply and the >> three accompanying vignettes. >> >> > library(zoo) >> > z <- zoo(1:10) >> > rollapply(z, 3, sum) >> >> 2 3 4 5 6 7 8 9 >> 6 9 12 15 18 21 24 27 >> >> On Sat, Sep 27, 2008 at 10:45 AM, milicic.marko <milicic.ma... at gmail.com> wrote: >> > Is there an implementation of moving window functionality so I can >> > apply any function while sliding trough window. >> >> > Thanks >> >> > ______________________________________________ >> > R-h... at r-project.org mailing list >> >https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> ______________________________________________ >> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >