Reputation: 5155
I have a huge vector of timestamps (double
type in R
). This is a column in my data.table
object. I want to divide these observations into n-minutes intervals - I want to have a character
vector representing e.g. time of the first value for this interval.
For example, for 7-minutes (7*60 seconds) interval I may have:
> ts <- c(1400132530, 1400134830)
> ts.intv <- ts - (ts %% (7*60))
>
> POSIXct.intv <- as.POSIXct(ts.intv, origin="1970-01-01")
> format(POSIXct.intv, "%H:%M:%S")
[1] "07:36:00" "08:18:00"
I tried to use sapply
operation fot this procedure but it is very time-consuming over my vector of timestamps (length ~ 15kk). Can anyone sugest better solution? Any built-in function?
Upvotes: 0
Views: 644
Reputation: 54237
What about cut
:
(ts <- seq.POSIXt(from=Sys.time(), by="2 mins", length.out=8))
# [1] "2014-06-07 00:51:10 CEST" "2014-06-07 00:53:10 CEST"
# [3] "2014-06-07 00:55:10 CEST" "2014-06-07 00:57:10 CEST"
# [5] "2014-06-07 00:59:10 CEST" "2014-06-07 01:01:10 CEST"
# [7] "2014-06-07 01:03:10 CEST" "2014-06-07 01:05:10 CEST"
groups <- cut.POSIXt(ts, breaks="7 mins")
levels(groups)
# [1] "2014-06-07 00:48:00" "2014-06-07 00:55:00"
Upvotes: 4