Learn R Programming

fred (version 0.3.1)

fred_interpolate: Fill missing values in a FRED series

Description

Fills NA values in the value column (long format) or in numeric columns (wide format). Two methods are supported: last-observation-carry-forward ("locf") and linear interpolation between adjacent observed values ("linear"). Use this for mixed-frequency analysis where a low-frequency series needs to be interpolated to a higher frequency.

Usage

fred_interpolate(data, method = c("locf", "linear"))

Value

A fred_tbl with interior NAs filled (see boundary note above).

Arguments

data

A fred_tbl or data.frame with a date column.

method

Character. "locf" (default) carries the last observed value forward. "linear" does linear interpolation between adjacent non-NA values.

Details

Boundary behaviour: with method = "locf", leading NAs remain NA because there is no prior observation to carry forward. With method = "linear", neither leading nor trailing NAs are filled because stats::approx() is called with rule = 1 (no extrapolation). If you need extrapolation, post-process the result.

See Also

Other utilities: fred_aggregate(), fred_event_window()

Examples

Run this code
# Synthetic example: fill interior NAs
d <- seq(as.Date("2024-01-01"), by = "month", length.out = 6L)
df <- data.frame(date = d, series_id = "X",
                 value = c(NA, 2, NA, NA, 5, NA))
fred_interpolate(df, method = "locf")
fred_interpolate(df, method = "linear")

# \donttest{
op <- options(fred.cache_dir = tempdir())
try({
  if (FALSE) {
  gdp <- fred_series("GDPC1", from = "2020-01-01")
  gdp_monthly <- fred_interpolate(gdp, method = "linear")
  }
})
options(op)
# }

Run the code above in your browser using DataLab