Learn R Programming

fred (version 0.3.1)

fred_aggregate: Aggregate FRED observations to a coarser frequency

Description

Aggregates a long-format fred_tbl (with date, series_id, value) or a wide-format fred_tbl (date plus one column per series) to a coarser calendar frequency. For long format, aggregation is performed per series_id; for wide format, per numeric column.

Usage

fred_aggregate(data, fun = "mean", by = "month")

Value

A fred_tbl with the same columns as the input, with date

collapsed to period start.

Arguments

data

A fred_tbl or data.frame with a date column.

fun

Character. Aggregation function. One of "mean", "sum", "first", "last", "median", "min", "max". Default "mean".

by

Character. Target frequency. One of "week", "month", "quarter", "year". Default "month".

Details

Use this when you have, say, daily Treasury yields and need a monthly average, or weekly initial claims aggregated to monthly totals. For server-side aggregation that mirrors FRED's own interpolation conventions, pass frequency = "m" to fred_series() instead.

See Also

Other utilities: fred_event_window(), fred_interpolate()

Examples

Run this code
# Synthetic example: aggregate daily synthetic data to monthly means
d <- seq(as.Date("2024-01-01"), as.Date("2024-06-30"), by = "day")
daily <- data.frame(date = d, series_id = "X", value = rnorm(length(d)))
fred_aggregate(daily, fun = "mean", by = "month")

# Wide-format input also works
wide <- data.frame(date = d, A = rnorm(length(d)), B = rnorm(length(d)))
fred_aggregate(wide, fun = "sum", by = "quarter")

# \donttest{
op <- options(fred.cache_dir = tempdir())
try({
  if (FALSE) {
  daily_yields <- fred_series("DGS10", from = "2023-01-01")
  monthly_yields <- fred_aggregate(daily_yields, fun = "mean", by = "month")
  }
})
options(op)
# }

Run the code above in your browser using DataLab