tsExtract: Extract Dates and Values from a Time Series

View source: R/tsExtract.R

tsExtractR Documentation

Extract Dates and Values from a Time Series

Description

Converts a base R time series object of class ts into two data frames — one of Date values and one of numeric values — suitable for passing directly to XY as the x and y arguments.

XY no longer accepts a ts object directly. Use tsExtract to prepare the components first, then pass them to XY. Because the extracted vectors exist in the global environment rather than in a data frame, include data=NULL in the XY call.

Usage

tsExtract(y.ts, x.name=NULL)

Arguments

y.ts

A time series object of class ts, either univariate or multivariate.

x.name

Optional character string used to name the date column in the returned x.dates data frame. When NULL, the column is named x.dates.

Details

The function uses time to obtain the fractional-year time index of each observation and converts it to a Date object. Supported frequencies are:

365 or 366 Daily
52 Weekly
12 Monthly
4 Quarterly
1 Yearly

Monthly conversion maps each fractional-year index to the first day of the corresponding calendar month, which sidesteps the uneven month lengths that base R's ts indexing does not track.

For a multivariate ts object, the y component contains one column per time series, named after the original column names.

Value

A list with three components:

x.dates

A single-column data frame of class Date, one row per observation.

y

A data frame of numeric values. One column for a univariate series; one column per series for a multivariate object.

y.name

Character string giving the name of the y variable (or the name from the yname option for a univariate series).

Author(s)

David W. Gerbing (Portland State University; gerbing@pdx.edu)

See Also

XY, ts, time

Examples

# Monthly airline passenger data (built-in R dataset)
pull <- tsExtract(AirPassengers)
x <- pull$x.dates
y <- pull$y
XY(x,y, data=NULL)

# Quarterly earnings data
pull <- tsExtract(JohnsonJohnson)
x <- pull$x.dates
y <- pull$y
XY(x, y, data=NULL)

lessR documentation built on June 21, 2026, 5:06 p.m.