tags:

views:

66

answers:

2

Hello

I have a zoo series in R. I can choose between a chron or a POSIXct index.

How can I aggregate to 15min, taking the last element every 15min?

I know how to aggregate daily, writing as.Date, but not how to aggregate every 15min.

thanks.

+2  A: 

If I recall, this is documented in the zoo vignettes. Did you look there?

The xts package, which builds on zoo has helper functions -- see help(to.period) in particular and the to.minutes15 function.

Dirk Eddelbuettel
+1  A: 

Here are a couple of possibilities depending on what you want. Both make use of trunc.times from the chron package. The aggregate.zoo solution takes the last value within each 15 minute interval and labels it using the time at the beginning of the 15 minute interval so the times used are: 00:00:00, 00:15:00, 00:30:00 and 00:45:00. The duplicated solution uses the same values but labels them using the last time actually found in the data. In both cases we only include intervals for which data is present.

There are more examples of aggregate.zoo in (1) ?aggregate.zoo, (2) all three of the zoo vignettes have examples and (3) searching the r-help archives for the words aggregate.zoo and trunc finds even more examples.

library(zoo)
library(chron)
z <- zoo(1:10, chron(1:10/(24*13)))

# 1. last value in each 15 minute interval 
# using time at which interval begins

aggregate(z, trunc(time(z), "00:15:00"), tail, 1)

# 2. last value in each 15 minute interval 
# time of last point in data within interval

z[!duplicated(trunc(time(z), "00:15:00"), fromLast = TRUE)]
G. Grothendieck
HiWhat I need is not"time at which interval begins" nor "time of last point in data within interval" but "time at which interval ends" an analogy is ... I need ceiling instead of floor or truncate
I've thought a possible solution:Adding 14 minutes and then truncating the result.What do you think?(I suppose my number of secs are 00)The problem would happen when the year changes and some leap seconds are added.
Yes, delta <- as.numeric(times("00:14:59")); aggregate(z, trunc(time(z) + delta, "00:15:00"), tail, 1) should do it.
G. Grothendieck