[R] Returning highs and lows in R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Oct 29 01:59:12 CEST 2010


On Thu, Oct 28, 2010 at 2:02 PM, Jason Kwok <jaykwok at gmail.com> wrote:
> I was able to get what I wanted using the lag function to offset an addition
> period.
>
> lag(rollapply(xx,3,max),-2) or lag(rollapply(xx,3,max,align="right"),-1)
>

Another possibility is to apply the function over the last 4 points
including the current point and then throw away the last one before
taking the max:

> library(zoo)
> z <- zoo(101:110)
> rollapply(z, 4, function(x) max(x[-4]), align = "right")
  4   5   6   7   8   9  10
103 104 105 106 107 108 109


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list