Maybe you are looking for tidyr::complete
to complete missing hours. This creates hourly sequence of 24 hours starting from first
value of time.
library(dplyr)
df %>%
mutate(V2 = as.POSIXct(V2, format = "%d-%m-%Y %H:%M")) %>%
arrange(V2) %>%
tidyr::complete(V2 = seq(first(V2), first(V2) + 86400 - (60 * 60),by = "1 hour"),
fill = list(V1 = 0, V3 = 0))
# V2 V1 V3
# <dttm> <dbl> <dbl>
# 1 2015-01-01 00:00:00 0 72
# 2 2015-01-01 01:00:00 1 74
# 3 2015-01-01 02:00:00 2 75
# 4 2015-01-01 03:00:00 3 77
# 5 2015-01-01 04:00:00 0 0
# 6 2015-01-01 05:00:00 0 0
# 7 2015-01-01 06:00:00 4 72
# 8 2015-01-01 07:00:00 0 0
# 9 2015-01-01 08:00:00 0 0
#10 2015-01-01 09:00:00 0 0
# … with 14 more rows
If the time doesn't start at 00:00
, we can extract the date from date-time and create a sequence of 24 hours.
df %>%
mutate(V2 = as.POSIXct(V2, format = "%d-%m-%Y %H:%M", tz = "GMT")) %>%
tidyr::complete(V2 = seq(as.POSIXct(as.Date(first(V2))),by = "1 hour",
length.out = 24), fill = list(V1 = 0, V3 = 0))
data
df <- structure(list(V1 = 0:4, V2 = structure(1:5, .Label = c("01-01-201500:00",
"01-01-201501:00", "01-01-201502:00", "01-01-201503:00", "01-01-201506:00"
), class = "factor"), V3 = c(72L, 74L, 75L, 77L, 72L)), class =
"data.frame", row.names = c(NA, -5L))