[R] aggregating specific parts in zoo index column to perform sliding average

Gabor Grothendieck ggrothendieck at gmail.com
Tue Feb 28 13:37:52 CET 2012


On Mon, Feb 27, 2012 at 10:17 PM, knavero <knavero at gmail.com> wrote:
> Here's my code:
>
> http://pastebin.com/0yRxEVtm
>
> The important parts are uncommented and should be easy to find using the
> link above. For the following line of code, I plan on looking for a way to
> offset it up 7 rows so that the 15 minute timestamp would be considered the
> "median" of the subset being averaged to find the mean:
>
> avgCool = aggregate(intCool, trunc(time(intCool), times("00:15:00")), mean)
>
> Currently the issue is that, with the truncate function, it "truncates" but
> really rounds down the time series values to the 15 minute time stamp
> earlier in the series. For example, let's say we have one minute intervals
> 0:00, 0:01, 0:02,....,0:37. It takes 0:00 - 0:14 and replaces that with
> 0:00. Then it sees 0:15, and changes values from 0:15 - 0:29 to 0:15. In
> effect, aggregating the values and creating subsets.
>
> What I want to do here is change 0:00 - 0:07 to 0:00, change 0:08 - 0:22 to
> 0:15, and change 0:23 - 0:37 to 0:30 in which 0:15 and 0:30 are the medians
> of each subset. Anyway, I hope that makes sense. Any ideas on which function
> will make this an easy job? Much thanks in advance.

Add 7.5 minutes before truncating so if x is your times, e.g. x <-
times(0:15/(24 * 60)), then try this:

min15 <- times("00:15:00")
trunc(x + min15/2, min15)


-- 
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