First, create some test data:
library(xts) # also pulls in zoo library(timeDate) library(chron) # includes times class # test data x <- xts(1:3, timeDate(c("2001-01-01 09:30:00", "2001-01-01 09:31:00", "2001-01-02 09:30:00")))
1) aggregate.zoo . Now try converting it to the times
class and aggregate using this one-line interface:
aggregate(as.zoo(x), times(format(time(x), "%H:%M:%S")), mean)
1a) aggregate.zoo (change) . or this option, which converts shorter rows of aggregates to times
, to avoid having to do this in a longer original series:
ag <- aggregate(as.zoo(x), format(time(x), "%H:%M:%S"), mean) zoo(coredata(ag), times(time(ag)))
2) click . An alternative would be tapply
, which is most likely faster:
ta <- tapply(coredata(x), format(time(x), "%H:%M:%S"), mean) zoo(unname(ta), times(names(ta)))
EDIT: Simplified (1) and Added (1a) and (2)
source share