time_aggregate {timeplyr} | R Documentation |
Aggregate time to a higher unit
Description
Aggregate time to a higher unit for possibly many groups with respect to a time index.
Usage
time_aggregate(
x,
time_by = NULL,
from = NULL,
to = NULL,
time_type = getOption("timeplyr.time_type", "auto"),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA"),
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1),
as_interval = getOption("timeplyr.use_intervals", FALSE)
)
Arguments
x |
Time vector. |
time_by |
Time unit.
|
from |
Start. |
to |
End. |
time_type |
If "auto", |
roll_month |
Control how impossible dates are handled when month or year arithmetic is involved. |
roll_dst |
See |
time_floor |
Should |
week_start |
day on which week starts following ISO conventions - 1
means Monday (default), 7 means Sunday.
This is only used when |
as_interval |
Should result be a |
Details
time_aggregate
aggregates time using
distinct moving time range blocks of a specified time unit.
The actual calculation is extremely simple and essentially requires a subtraction, a rounding and an addition.
To perform a by-group time aggregation, simply supply
collapse::fmin(x, g = groups, TRA = "replace_fill")
as the
from
argument.
Value
A time_interval
.
See Also
Examples
library(timeplyr)
library(nycflights13)
library(lubridate)
library(dplyr)
sunique <- function(x) sort(unique(x))
hours <- sunique(flights$time_hour)
days <- as_date(hours)
# Aggregate by week or any time unit easily
sunique(time_aggregate(hours, "week"))
sunique(time_aggregate(hours, ddays(14)))
sunique(time_aggregate(hours, "month"))
sunique(time_aggregate(days, "month"))
# Left aligned
sunique(time_aggregate(days, "quarter"))
# Very fast by group aggregation
start <- collapse::fmin(flights$time_hour, g = flights$tailnum,
TRA = "replace_fill")
week_by_tailnum <- time_aggregate(flights$time_hour, time_by = ddays(7),
from = start)
# Confirm this has been done by group as each group will have a
# Different aggregate start date
flights %>%
stat_summarise(week_by_tailnum, .by = tailnum, stat = "min",
sort = FALSE)