time_by {timeplyr} | R Documentation |
Group by a time variable at a higher time unit
Description
time_by
groups a time variable by a specified time unit like
for example "days" or "weeks".
It can be used exactly like dplyr::group_by
.
Usage
time_by(
data,
time,
time_by = NULL,
from = NULL,
to = NULL,
.name = paste0("time_intv_", time_by_pretty(time_by, "_")),
.add = FALSE,
time_type = getOption("timeplyr.time_type", "auto"),
as_interval = getOption("timeplyr.use_intervals", FALSE),
.time_by_group = TRUE
)
time_by_span(x)
time_by_var(x)
time_by_units(x)
Arguments
data |
A data frame. |
time |
Time variable (data-masking). |
time_by |
Time unit.
|
from |
(Optional) Start time. |
to |
(Optional) end time. |
.name |
An optional glue specification passed to |
.add |
Should the time groups be added to existing groups?
Default is |
time_type |
If "auto", |
as_interval |
Should time variable be a |
.time_by_group |
Should the time aggregations be built on a
group-by-group basis (the default), or should the time variable be aggregated
using the full data? If done by group, different groups may contain
different time sequences. This only applies when |
x |
A |
Value
A time_tbl_df
which for practical purposes can be treated the
same way as a dplyr grouped_df
.
Examples
library(dplyr)
library(timeplyr)
library(nycflights13)
library(lubridate)
# Basic usage
hourly_flights <- flights %>%
time_by(time_hour) # Detects time granularity
hourly_flights
time_by_span(hourly_flights)
monthly_flights <- flights %>%
time_by(time_hour, "month")
weekly_flights <- flights %>%
time_by(time_hour, "week", from = floor_date(min(time_hour), "week"))
monthly_flights %>%
count()
weekly_flights %>%
summarise(n = n(), arr_delay = mean(arr_delay, na.rm = TRUE))
# To aggregate multiple variables, use time_aggregate
flights %>%
select(time_hour) %>%
mutate(across(everything(), \(x) time_aggregate(x, time_by = "weeks"))) %>%
count(time_hour)