View source: R/time_aggregate.R
time_aggregate | R Documentation |
Aggregate time to a higher unit for possibly many groups with respect to a time index.
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", TRUE)
)
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 |
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.
A time_interval
.
time_summarisev time_cut
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")
flights %>%
mutate(start = collapse::fmin(time_hour, g = list(origin, dest), TRA = "replace_fill")) %>%
mutate(week = time_aggregate(time_hour, dweeks(1), from = start)) %>%
select(origin, dest, time_hour, week)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.