Converts a series, stored in a data.frame or tibble, into a ts object.
A ts positions observations by period rather than by date, so the input
must cover a complete run of periods. Rows are sorted before conversion, and
a missing or duplicated period is rejected rather than silently shifting
every later observation onto the wrong date. Missing values are kept in
place, which leaves the series correctly dated.
Usage
df_to_ts(x, date_col = "date", value_col = "value", frequency = 12)
Arguments
- x
A data.frame, tibble or data.table.
- date_col
Name of the date column. Defaults to 'date'. Must be of class Date.
- value_col
Name of the value column. Defaults to 'value'. Must be numeric.
- frequency
The frequency of the series. Can be a shortened string (e.g. "M" for monthly) or a number (e.g. 12).
Examples
ibc <- df_to_ts(ibcbr, value_col = "index", frequency = "M")
class(ibc)
#> [1] "ts"
plot(ibc)