Learn R Programming

gseries (version 3.0.2)

ts_to_tsDF: Convert a "ts" object to a time series data frame

Description

Convert a "ts" (or "mts") object into a time series data frame for the benchmarking functions with three or more variables (columns):

  • two (2) for the data point identification (year and period)

  • one (1) for each time series

Usage

ts_to_tsDF(
  in_ts,
  yr_cName = "year",
  per_cName = "period",
  val_cName = "value"
)

Value

The function returns a data frame with three or more variables:

  • Data point year, type numeric (see argument startYr_cName)

  • Data point period, type numeric (see argument startPer_cName)

  • One ("ts" object) or many ("mts" object) time series data variable(s), type numeric (see argument val_cName)

Note: the function returns a "data.frame" object than can be explicitly coerced to another type of object with the appropriate as*() function (e.g., tibble::as_tibble() would coerce it to a tibble).

Arguments

in_ts

(mandatory)

Time series (object of class "ts" or "mts") to be converted.

yr_cName, per_cName

(optional)

Strings specifying the name of the numeric variables (columns) in the output data frame that will contain the data point year and period identifiers.

Default values are yr_cName = "year" and per_cName = "period".

val_cName

(optional)

String specifying the name of the numeric variable (column) in the output data frame that will contain the data point value. This argument has no effect for "mts" objects (time series data variable names are automatically inherited from the "mts" object).

Default value is val_cName = "value".

See Also

tsDF_to_ts() ts_to_bmkDF() stack_tsDF() benchmarking() stock_benchmarking() time_values_conv

Examples

Run this code
# Quarterly time series
my_ts <- ts(1:10 * 100, start = 2019, frequency = 4)
my_ts


# With the default variable (column) names
ts_to_tsDF(my_ts)

# Using a custom name for the time series data variable (column)
ts_to_tsDF(my_ts, val_cName = "ser_val")


# Multiple time series: argument `val_cName` ignored
# (the "mts" object column names are always used)
ts_to_tsDF(ts.union(ser1 = my_ts,
                    ser2 = my_ts / 10),
            val_cName = "useless_column_name")

Run the code above in your browser using DataLab