Skip to contents

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.

Usage

aggregate_time(tbl, time = year, fun = sum, average = FALSE)

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 the lubridate functions year, month, day, or yday.

fun

Aggregation function (e.g. sum, mean, min, or max).

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.

Value

A tibble with the aggregated value columns and added time interval columns.

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)

}