library(timeplyr)
library(dplyr)
library(lubridate)
library(nycflights13)
# \dontshow{
.n_dt_threads <- data.table::getDTthreads()
.n_collapse_threads <- collapse::get_collapse()$nthreads
data.table::setDTthreads(threads = 1L)
collapse::set_collapse(nthreads = 1L)
# }
x <- flights$time_hour
time_num_gaps(x) # Missing hours
flights_count <- flights %>%
fastplyr::f_count(time_hour)
# Fill in missing hours
flights_count %>%
time_complete(time = time_hour)
# You can specify units too
flights_count %>%
time_complete(time = time_hour, time_by = "hours")
flights_count %>%
time_complete(time = as_date(time_hour), time_by = "days") # Nothing to complete here
# Where time_expand() and time_complete() really shine is how fast they are with groups
flights %>%
group_by(origin, dest) %>%
time_expand(time = time_hour, time_by = dweeks(1))
# \dontshow{
data.table::setDTthreads(threads = .n_dt_threads)
collapse::set_collapse(nthreads = .n_collapse_threads)
# }
Run the code above in your browser using DataLab