time_summarise {timeplyr} | R Documentation |
A time based extension to dplyr::summarise()
Description
This works much the same as dplyr::summarise()
, except that
you can supply an additional time
argument to allow for
aggregating time to a higher unit.
Usage
time_summarise(
data,
time = NULL,
...,
time_by = NULL,
from = NULL,
to = NULL,
time_type = getOption("timeplyr.time_type", "auto"),
include_interval = FALSE,
.by = NULL,
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "boundary"),
sort = TRUE
)
Arguments
data |
A data frame. |
time |
Time variable. |
... |
Additional variables to include. |
time_by |
Time unit.
|
from |
Time series start date. |
to |
Time series end date. |
time_type |
If "auto", |
include_interval |
Logical. If |
.by |
(Optional). A selection of columns to group by for this operation. Columns are specified using tidy-select. |
time_floor |
Should |
week_start |
day on which week starts following ISO conventions - 1
means Monday, 7 means Sunday (default).
This is only used when |
roll_month |
Control how impossible dates are handled when
month or year arithmetic is involved.
Options are "preday", "boundary", "postday", "full" and "NA".
See |
roll_dst |
See |
sort |
Should the result be sorted? Default is |
Value
A summarised data.frame
.
Examples
library(timeplyr)
library(dplyr)
library(lubridate)
library(nycflights13)
# Works the same way as summarise()
# Monthly average arrival time
flights %>%
mutate(date = as_date(time_hour)) %>%
time_summarise(mean_arr_time = mean(arr_time, na.rm = TRUE),
time = date,
time_by = "month",
include_interval = TRUE)
# Example of monthly summary using zoo's yearmon
flights %>%
mutate(yearmon = zoo::as.yearmon(as_date(time_hour))) %>%
time_summarise(time = yearmon,
n = n(),
mean_arr_time = mean(arr_time, na.rm = TRUE),
include_interval = TRUE)