Learn R Programming

fred (version 0.3.1)

fred_event_window: Extract data windows around event dates

Description

Given a fred_tbl (or any data frame with a date column) and a vector of event dates, returns one row per (event, observation) pair where the observation falls inside the requested window. Useful for event studies around FOMC decisions, recession peaks, or release dates.

Usage

fred_event_window(data, events, window = c(-30L, 90L))

Value

A fred_tbl with the original columns plus event_date and days_from_event.

Arguments

data

A fred_tbl or data.frame with a date column.

events

A character or Date vector of event dates.

window

Integer length-2. Days before (negative or zero) and after (positive or zero) each event date. Default c(-30L, 90L).

Details

Long-format input with series_id/value is supported, as is wide-format input from fred_series(..., format = "wide"). The window is in calendar days; for monthly or quarterly data, choose a window large enough to capture at least one observation.

See Also

Other utilities: fred_aggregate(), fred_interpolate()

Examples

Run this code
# Synthetic example — works offline
d <- seq(as.Date("2024-01-01"), as.Date("2024-12-31"), by = "month")
df <- data.frame(date = d, value = seq_along(d))
events <- as.Date(c("2024-03-15", "2024-09-15"))
fred_event_window(df, events = events, window = c(-30L, 60L))

# \donttest{
op <- options(fred.cache_dir = tempdir())
try({
  # With live FRED data: UNRATE around 2024 SEP meetings (needs API key)
  if (FALSE) {
  ur <- fred_series("UNRATE", from = "2023-01-01")
  sep <- fred_fomc_dates(year = 2024, sep_only = TRUE)
  fred_event_window(ur, events = sep$date, window = c(-60L, 60L))
  }
})
options(op)
# }

Run the code above in your browser using DataLab