AggregateByDate: Aggregate values by date

View source: R/aggregatebydate.R

AggregateByDateR Documentation

Aggregate values by date

Description

Aggregates values by date. Calls tapply.

Usage

AggregateByDate(x, by, dates, FUN = sum, ...)

Arguments

x

A vector containing the values to be aggregated, or, a 1D-array, or some other structure with dates in the first column

by

The period used in the conversion (e.g., "week", "year").

dates

Either dates or characters that can be coerced to dates. Inferred from x if not suppled (e.g., the names of x).

FUN

The function to be applied, or NULL. In the case of functions like +, If FUN is NULL, tapply returns a vector which can be used to subscript the multi-way array tapply normally produces.

...

Optional arguments to FUN.

Value

A vector of aggregated values.

Examples

z <- rep(1, 54)
dts <- seq.Date(as.Date("2017/01/01"), by = "week", length.out = length(z))
AggregateByDate(z, dates = dts, by = "year")

z <- seq_along(dts)
names(z) <- dts
AggregateByDate(z, by = "year", FUN = mean)

data(presidents, package = "datasets")
origin <- paste0(start(presidents)[1], "-01-01")
df <- data.frame(dates = seq.Date(as.Date(0, origin), length.out = length(presidents),
                                                           by = "quarters"),
                             x = as.numeric(presidents))
AggregateByDate(df, by = "year", FUN = median, na.rm = TRUE)

NumbersInternational/flipTime documentation built on Feb. 26, 2024, 6:44 a.m.