Learn R Programming

tsbox (version 0.4.1)

ts_frequency: Change Frequency

Description

Changes the frequency of a time series. By default, incomplete periods of regular series are omitted.

Usage

ts_frequency(
  x,
  to = c("year", "quarter", "month", "week", "day", "hour", "min", "sec"),
  aggregate = "mean",
  na.rm = FALSE
)

Value

a ts-boxable time series, with the same class as the input.

Arguments

x

ts-boxable time series, an object of class ts, xts, zoo, zooreg, data.frame, data.table, tbl, tbl_ts, tbl_time, tis, irts or timeSeries.

to

desired frequency, either a character string ("year", "quarter", "month") or an integer (1, 4, 12).

aggregate

character string, or function. Either "mean", "sum", "first", or "last", or any aggregate function, such as base::mean().

na.rm

logical, if TRUE, incomplete periods are aggregated as well. For irregular series, incomplete periods are always aggregated.

Details

The tempdisagg package can convert low frequency to high frequency data and has support for ts-boxable objects. See vignette("hf-disagg", package = "tempdisagg").

Examples

Run this code
# \donttest{
ts_frequency(cbind(mdeaths, fdeaths), "year", "sum")
ts_frequency(cbind(mdeaths, fdeaths), "quarter", "last")

ts_frequency(AirPassengers, 4, "sum")

# Note that incomplete years are omited by default
ts_frequency(EuStockMarkets, "year")
ts_frequency(EuStockMarkets, "year", na.rm = TRUE)
# }

Run the code above in your browser using DataLab