(I've read many similar problems, but could not find an answer to my problem, even though it seems trivial; I just don't see the cause of the problem)
I have time-stamped measurements (with some offset-based numeric timestamps), and I want to aggregate at different time intervals for analysis. For readability I converted the time stamps to human-readable ones (using as.POSIXlt()
with origin
and tz="UTC"
parameters).
On integer values cut()
worked well, but I don't get it working for POSIXlt
values. According to the manual (and some answers I found), it should work out-of-the-box.
Browse[1]> str(cols$time)
List of 39531
$ : POSIXct[1:1], format: "2016-07-05 00:00:58"
$ : POSIXct[1:1], format: "2016-07-05 00:02:02"
$ : POSIXct[1:1], format: "2016-07-05 00:03:06"
$ : POSIXct[1:1], format: "2016-07-05 00:04:10"
$ : POSIXct[1:1], format: "2016-07-05 00:05:14"
$ : POSIXct[1:1], format: "2016-07-05 00:06:18"
[...]
$ : POSIXct[1:1], format: "2016-07-05 01:44:26"
$ : POSIXct[1:1], format: "2016-07-05 01:45:30"
[list output truncated]
Browse[1]> cut(cols$time, breaks="5 min")
Fehler in cut.default(cols$time, breaks = "5 min") :
'x' muss numerisch sein
Browse[1]> cut.POSIXt(cols$time, breaks="5 min")
Fehler in cut.POSIXt(cols$time, breaks = "5 min") :
'x' muss ein date-time Objekt sein
Browse[1]>
(Sorry for the German locale (and error messages))
I want R to subdivide the total time interval into either n
equally long intervals, or intervals of a specific duration.
Additional Info:
I wrote this helper function (actually the maths are a bit more complicated, but the principle is this):
POSIX <- function(day, offset) {
origin <- "1970-01-01 00:00:00"
return(as.POSIXlt(day * 86400 + offset, origin=origin, tz="UTC"))
}
(Receives day number and offset within day, returning a POSIXlt
timestamp)
The final timestamps themselves are created in a two-way process:
for (d in dayLevels) {
days[[d]] <- POSIX(as.numeric(d), 0)
}
cols$time <- lapply(1:rows, function(i) return(cols$day[[i]] + cols$time[i])
(I'm adding fractional seconds to the POSIXlt
)