I do not believe that there is a way to do this, built into the zoo or cron, but you can create your own function using a little math. Here you are:
trunc.minutes <- function (x, n.minutes) { if (!inherits(x, "times")) x <- as.chron(x) x <- as.numeric(x) sec <- round(24 * 3600 * abs(x - floor(x))) h <- (sec%/%(n.minutes*60))/(60/n.minutes) hour <- as.integer(h) minutes <- (h %% hour) * 60 chron(dates=chron::dates(x), times=times(paste(hour, minutes, "00", sep=":"))) }
Here is a usage example:
dts <- chron::dates(c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92")) tms <- times(c("23:03:20", "23:29:56", "01:03:30", "18:21:03", "16:56:26")) x <- chron(dates = dts, times = tms)
Finally, you can now use this function to aggregate data:
ts.zoo <- zoo(rnorm(5), x)
Or simply use these new dates for aggregation (see how it updates the second example, since there are two values ββin this window):
> aggregate(ts.zoo, trunc.minutes(x, 15), mean) (01/14/92 01:00:00) (02/01/92 16:45:00) (02/27/92 23:00:00) (02/27/92 23:15:00) -0.6738659 -0.4844803 0.7968155 -1.3571121 (02/28/92 18:15:00) 0.7625861 > aggregate(ts.zoo, trunc.minutes(x, 30), mean) (01/14/92 01:00:00) (02/01/92 16:30:00) (02/27/92 23:00:00) (02/28/92 18:00:00) -0.6738659 -0.4844803 -0.2801483 0.7625861
Shane
source share