View source: R/dplyr-summarise_by_time.R
summarise_by_time | R Documentation |
summarise_by_time()
is a time-based variant of the popular dplyr::summarise()
function
that uses .date_var
to specify a date or date-time column and .by
to group the
calculation by groups like "5 seconds", "week", or "3 months".
summarise_by_time()
and summarize_by_time()
are synonyms.
summarise_by_time( .data, .date_var, .by = "day", ..., .type = c("floor", "ceiling", "round"), .week_start = NULL ) summarize_by_time( .data, .date_var, .by = "day", ..., .type = c("floor", "ceiling", "round"), .week_start = NULL )
.data |
A |
.date_var |
A column containing date or date-time values to summarize. If missing, attempts to auto-detect date column. |
.by |
A time unit to summarise by.
Time units are collapsed using The value can be:
Arbitrary unique English abbreviations as in the |
... |
Name-value pairs of summary functions. The name will be the name of the variable in the result. The value can be:
|
.type |
One of "floor", "ceiling", or "round. Defaults to "floor". See |
.week_start |
when unit is weeks, specify the reference day. 7 represents Sunday and 1 represents Monday. |
A tibble
or data.frame
Sum: sum()
Center: mean()
, median()
Spread: sd()
, var()
Range: min()
, max()
Count: dplyr::n()
, dplyr::n_distinct()
Position: dplyr::first()
, dplyr::last()
, dplyr::nth()
Correlation: cor()
, cov()
Time-Based dplyr functions:
summarise_by_time()
- Easily summarise using a date column.
mutate_by_time()
- Simplifies applying mutations by time windows.
filter_by_time()
- Quickly filter using date ranges.
filter_period()
- Apply filtering expressions inside periods (windows)
between_time()
- Range detection for date or date-time sequences.
pad_by_time()
- Insert time series rows with regularly spaced timestamps
condense_period()
- Convert to a different periodicity
slidify()
- Turn any function into a sliding (rolling) function
# Libraries library(timetk) library(dplyr) # First value in each month m4_daily %>% group_by(id) %>% summarise_by_time( .date_var = date, .by = "month", # Setup for monthly aggregation # Summarization value = first(value) ) # Last value in each month (day is first day of next month with ceiling option) m4_daily %>% group_by(id) %>% summarise_by_time( .by = "month", value = last(value), .type = "ceiling" ) %>% # Shift to the last day of the month mutate(date = date %-time% "1 day") # Total each year (.by is set to "year" now) m4_daily %>% group_by(id) %>% summarise_by_time( .by = "year", value = sum(value) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.