time_expand | R Documentation |
tidyr::complete()
.A time based extension to tidyr::complete()
.
time_expand(
data,
time = NULL,
...,
.by = NULL,
time_by = NULL,
from = NULL,
to = NULL,
sort = TRUE
)
time_complete(
data,
time = NULL,
...,
.by = NULL,
time_by = NULL,
from = NULL,
to = NULL,
sort = TRUE,
fill = NULL
)
data |
A data frame. |
time |
Time variable. |
... |
Groups to expand. |
.by |
(Optional). A selection of columns to group by for this operation. Columns are specified using tidy-select. |
time_by |
A timespan. |
from |
Time series start date. |
to |
Time series end date. |
sort |
Logical. If |
fill |
A named list containing value-name pairs to fill the named implicit missing values. |
This works much the same as tidyr::complete()
, except that
you can supply an additional time
argument to allow for
completing implicit time gaps and creating time sequences by group.
A data.frame
of expanded time by or across groups.
library(timeplyr)
library(dplyr)
library(lubridate)
library(nycflights13)
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.