r - Adding missing time values -
i have table gives me date-time have received data , count of how data received in thirty minute interval. problem half hour blocks missing, , want insert them column , insert 0 in count column.
here example of table looks like:
date-time count 2017-07-13 17:30:00 111 2017-07-13 18:00:00 85 2017-07-13 20:00:00 127 2017-07-13 20:30:00 515
i want have 18:30:00 0 , on
not sure how if has idea great.
here have tried do:
starttime <- df[1,`date-time`] (i in df){ time <- starttime + 30 new_dt$datetime <- ifelse(df[i] = time, df$datetime, time) new_dt$count <- ifelse(df[i] = time, df$count, 0) }
first let's create dummy data.
library(tidyverse) library(lubridate) time_series <- tibble( datetime = c( "2017-07-13 17:30:00", "2017-07-13 18:00:00", "2017-07-13 20:00:00", "2017-07-13 20:30:00" ), count = c(111, 85, 127, 515) ) %>% mutate(datetime = ymd_hms(datetime))
now let's figure out smallest , largest datetimes have in data.
from <- min(time_series$datetime) <- max(time_series$datetime)
finally, let's create sequence of dates from
to
@ 30 minute intervals. join existing data sequence , replace missing values of count
zero.
tibble(datetime = seq(from = from, = to, = 1800)) %>% left_join(time_series) %>% mutate(count = ifelse(is.na(count), 0, count))
Comments
Post a Comment