aggregate_time()
aggregates tables with a date
column and multiple value
columns to coarser time steps, which are defined by time
using the function
defined by fun
.
Arguments
- tbl
A data frame that contains a 'date' column and multiple numerical columns.
- time
A function or a vector of functions to define the time intervals to which
tbl
is aggregated. Must be one or more of thelubridate
functionsyear
,month
,day
, oryday
.- fun
Aggregation function (e.g.
sum
,mean
,min
, ormax
).- average
Should values be averaged after the aggregation? When e.g. the time interval is defined by
time = c(year, month)
,average = TRUE
, will first aggregate to year and month and then average to mean monthy values.
Examples
if (FALSE) {
library(tidyverse)
library(lubridate)
# Generate dummy table
date <- seq(ymd(19700101), ymd(20101231), by = 'day')
n <- 100
tbl <- map(1:n, ~ runif(length(date))) %>%
set_names(id_to_run(1:n)) %>%
bind_cols(.)
tbl <- bind_cols(date = date, tbl)
# Calculate maximum values for days of the year
aggregate_time(tbl, time = yday, fun = max)
# Calculate mean monthly sums
aggregate_time(tbl, time = c(year, month), fun = sum, average = TRUE)
# Calculate average annual sums
aggregate_time(tbl, time = year, fun = sum, average = TRUE)
}