I have a dataframe with varying time steps, which I want to convert into even time steps. Every 10 minutes a value should be written and if there is no new value, the previous one should be taken (see 2019-01-01 01:00:00 and 2019-01-01 02:30:00).
date ZUL_T
1 2019-01-01 00:04:00 23.3
2 2019-01-01 00:15:00 23.3
3 2019-01-01 00:26:00 19.9
4 2019-01-01 00:37:00 20.7
5 2019-01-01 00:48:00 21.9
6 2019-01-01 00:59:00 21.9
7 2019-01-01 01:10:00 18.8
8 2019-01-01 01:22:00 18.8
9 2019-01-01 01:33:00 20.7
10 2019-01-01 01:44:00 21.6
11 2019-01-01 01:55:00 19.2
12 2019-01-01 02:06:00 19.2
13 2019-01-01 02:17:00 19.6
14 2019-01-01 02:29:00 19.6
15 2019-01-01 02:40:00 20.5
This is my current code, but there are some time steps missing if there is no value in the DS.
library(lubridate)
lowtime <- min(DS$date)
hightime <- max(DS$date)
# Set the minute and second to the nearest 10 minute value
minute(lowtime) <- floor(minute(lowtime)/10) * 10
minute(hightime) <- ceiling(minute(hightime)/10) * 10
second(lowtime) <- 0
second(hightime) <- 0
# Set the breakpoints at 10 minute intervals
breakpoints <- seq.POSIXt(lowtime, hightime, by = 600)
ZUL_T <- aggregate(ZUL_T ~ cut(date, breaks = breakpoints), DS, mean)
> data
date ZUL_T
1 2019-01-01 00:00:00 23.3
2 2019-01-01 00:10:00 23.3
3 2019-01-01 00:20:00 19.9
4 2019-01-01 00:30:00 20.7
5 2019-01-01 00:40:00 21.9
6 2019-01-01 00:50:00 21.9
7 2019-01-01 01:10:00 18.8
8 2019-01-01 01:20:00 18.8
9 2019-01-01 01:30:00 20.7
10 2019-01-01 01:40:00 21.6
11 2019-01-01 01:50:00 19.2
12 2019-01-01 02:00:00 19.2
13 2019-01-01 02:10:00 19.6
14 2019-01-01 02:20:00 19.6
15 2019-01-01 02:40:00 20.5