[R] reverse direction with rollapply()

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 22 01:07:28 CEST 2010


On Wed, Jul 21, 2010 at 6:59 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hello,
>
> I am using rollapply() from package zoo to use a function on unique
> windows from a dataset.  Because my dataset is not a multiple of my
> window width, it matters which end I start at.  Here is a simple
> example, the result I want is 7.5 (i.e., start at the highest level of
> my ordering variable).  I thought that the argument ascending = FALSE
> would do it, but it does not seem to have an effect.  It seems like I
> must be missing something simple.
>
> #Library and sample data
> library(zoo)
> mydata <- zoo(x = 6:8, order.by = 1:3)
>
> rollapply(data = mydata, width = 2, by = 2,
>  FUN = mean, ascending = TRUE, align = "left")
>
> #This gives the same results
> rollapply(data = mydata, width = 2, by = 2,
>  FUN = mean, ascending = FALSE, align = "left")
>
>

ascending only specifies the order to pass each set of points but does
not change the sets themselves.  Try this:


> rollapply(data = mydata[-1], width = 2, by = 2, FUN = mean)
  2
7.5



More information about the R-help mailing list