[R] Returning highs and lows in R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Oct 29 06:02:06 CEST 2010


On Thu, Oct 28, 2010 at 7:59 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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

Also, in the devel version of zoo available on R-Forge there is a new
which= argument which can be used to specify which relative positions
to use.  Using z from above:

> # devel version of zoo from R-Forge
> rollapply(z, which = c(-3, -2, -1), FUN = max)
  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