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) Thanks. Jason On Thu, Oct 28, 2010 at 1:49 PM, Jason Kwok <jayk...@gmail.com> wrote: > I figured out how to offset my observations by 1 period by using the > rollapply(xx,3,max,align="right"), which would calculate the mean for > (current observation, obs - 1 and obs -2 ). How would I further offset by > 1 more period? > > Thanks, > > Jason > > On Thu, Oct 28, 2010 at 1:29 PM, Jason Kwok <jayk...@gmail.com> wrote: > >> Thanks for the help. >> >> I'm looking to calculate rolling max and means for the last 3 observations >> in my data including AND not including the current observation. I'm not >> sure how to offset the observations used. >> >> For the 3 period max, I would like to return the max value over the last 3 >> observations not including today. When I use the rollapply function to take >> the max, it will look look back 1 observation, take the current observation >> and look forward 1 observation. I would like to return look back >> observations 1-3 and return the max. >> >> > merge(xx,rollapply(xx,3,max)) >> GLD.Close GLD.Close.1 >> 2010-04-01 110.26 NA >> 2010-04-05 110.89 111.03 >> 2010-04-06 111.03 112.49 >> 2010-04-07 112.49 112.65 >> 2010-04-08 112.65 113.64 >> 2010-04-09 113.64 113.64 >> 2010-04-12 113.01 113.64 >> 2010-04-13 112.69 113.03 >> 2010-04-14 113.03 113.65 >> 2010-04-15 113.65 113.65 >> 2010-04-16 111.24 113.65 >> 2010-04-19 111.15 111.46 >> 2010-04-20 111.46 112.31 >> 2010-04-21 112.31 112.31 >> 2010-04-22 111.84 113.19 >> 2010-04-23 113.19 113.19 >> 2010-04-26 112.75 114.63 >> 2010-04-27 114.63 114.63 >> 2010-04-28 114.31 114.63 >> 2010-04-29 114.28 115.36 >> 2010-04-30 115.36 NA >> >> >> On Thu, Oct 28, 2010 at 12:45 PM, Bert Gunter <gunter.ber...@gene.com>wrote: >> >>> Jason: >>> >>> Please read AND FOLLOW the posting guide on how to ask clear >>> questions. Here, you need to more carefully define what you mean by >>> "the last 3 days." Do you mean:(a) the last 3 values in the series >>> (including or excluding the present one?) or the last 3 calendar days >>> -- e.g. for 10-05, only 10-05 and 10-04, since 10-01 is not within the >>> last 3 calendar days.Also, do you have missing values, and, if so, how >>> do you want to handle them. >>> >>> If you mean the former, for small amounts of data without any >>> missings(say 100 million numeric values or less) and small n (like >>> n=3), it's easy and should be pretty fast just to produce lagged >>> columns and use pmax rowwise. If you mean the latter and have missing >>> values, it may be considerably more difficult. >>> >>> However, offering anything more seems pointless until you have >>> adequately specified what you want. Reproducible data and code for a >>> start. >>> >>> Cheers, >>> Bert >>> >>> On Thu, Oct 28, 2010 at 9:27 AM, Jason Kwok <jayk...@gmail.com> wrote: >>> > I'm having trouble returning a rolling n period highest value for a >>> data >>> > set. For each day I want to calculate the highest value over the last >>> 3 >>> > days. I am using the following packages: zoo, xts, quantmod and TTR. >>> > >>> > Thanks, Jason >>> > >>> > GLD.Close >>> > 2010-10-01 128.91 >>> > 2010-10-04 128.46 >>> > 2010-10-05 130.99 >>> > 2010-10-06 131.81 >>> > 2010-10-07 130.37 >>> > 2010-10-08 131.66 >>> > 2010-10-11 132.29 >>> > 2010-10-12 131.96 >>> > 2010-10-13 134.07 >>> > 2010-10-14 134.75 >>> > 2010-10-15 133.68 >>> > 2010-10-18 134.28 >>> > 2010-10-19 130.11 >>> > 2010-10-20 131.32 >>> > 2010-10-21 129.47 >>> > 2010-10-22 129.73 >>> > 2010-10-25 130.85 >>> > 2010-10-26 130.88 >>> > 2010-10-27 129.52 >>> >> >>> > >>> > [[alternative HTML version deleted]] >>> > >>> > ______________________________________________ >>> > R-help@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. >>> > >>> >>> >>> >>> -- >>> Bert Gunter >>> Genentech Nonclinical Biostatistics >>> >> >> > [[alternative HTML version deleted]] ______________________________________________ R-help@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.