I need to aggregate two POSIXct 5-min sequences into 10-min sequences. In order to do this I want to create a 10-min key from both 5-min sequences such that both use the same levels.
I'm wondering if there is an R way of doing this?
Here is an example:
a = seq(as.POSIXct("2012-06-01 06:01"), by = "5 min", length.out = 24)
b = seq(as.POSIXct("2012-06-01 06:07"), by = "5 min", length.out = 24)
cut(a, "10 min")
cut(b, "10 min")
You'll notice seq a and b use different levels
Now you can do some trickery to fix this, but its not really nice
b_adjusted = b-min(abs(min(as.POSIXct(cut(a, "10 min")))-b))
cut(b_adjusted, "10 min")
or
keys = cut(c(a,b), "10 min")
a = keys[1:length(a)]
b = keys[(length(a)+1):(length(a)+length(b))]
This is just an example, my actual case involves >10 sequences. Ideally i would be able to use cut with levels starting from the first round 10-min level eg if the first item in my sequence is 00:17 then the generated key is 00:10
Maybe combine all the vectors and then generate 10 min sequence from min
to max
value.
library(lubridate)
combined_seq <- c(a, b)
lvls <- seq(floor_date(min(combined_seq), '10 mins'),
ceiling_date(max(combined_seq), '10 mins'), by = '10 mins')
You can then use this lvls
as levels in cut
.
cut(a, levels)
cut(b, levels)