Question

I have a zoo time series as follows:

>> head(ww)
(11/22/08 11:37:00) (11/22/08 12:07:00) (11/22/08 12:22:00) (11/22/08 12:37:00) 
           0.087114            0.055422            0.055250            0.059483 
(11/22/08 12:52:00) (11/22/08 13:07:00) 
           0.057896            0.061808

As you can see, the times are 11:37, 12:07, 12:22 etc. I would like to change these times to the nearest whole multiple of ten minutes - for example XX:10, XX:20. In this case 11:37 would become 11:40, 12:07 would become 12:10 and 12:22 would become 12:20.

I have found the how to aggregate it to the nearest minute:

wholemin <- function(x) trunc(x, units="minutes")
result = aggregate(r, wholemin, head, 1)

But I can't use trunc to aggregate to anything other than seconds, minutes, hours etc.

How should I go about doing this?

For ease of examples, the output of dput(head(ww) is below:

structure(c(0.087114, 0.055422, 0.05525, 0.059483, 0.057896, 
0.061808), index = structure(c(14205.4840277778, 14205.5048611111, 
14205.5152777778, 14205.5256944444, 14205.5361111111, 14205.5465277778
), format = structure(c("m/d/y", "h:m:s"), .Names = c("dates", 
"times")), origin = structure(c(1, 1, 1970), .Names = c("month", 
"day", "year")), class = c("chron", "dates", "times")), class = "zoo")
Was it helpful?

Solution

Try this:

to10 <- function(tt) trunc(tt + as.numeric(times("00:05:00")), units = "00:10:00")
aggregate(z, to10, mean)

and see the examples in ?trunc.times and ?aggregate.zoo .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top