Learn R Programming

bsts (version 0.9.5)

wide.to.long: Convert Between Wide and Long Format

Description

Convert a multivariate time series between wide and long formats. In "wide" format there is one row per time point, with series organzied by columns. In "long" format there is one row per observation, with variables indicating the series and time point to which an observation belongs.

Usage

WideToLong(response, na.rm = TRUE)
  LongToWide(response, series.id, timestamps)

Arguments

response

For WideToLong this is a matrix, with rows representing time and columns representing variables. This can be a zoo matrix with timestamps as an index.

For LongToWide, response is a vector.

na.rm

If TRUE then missing values will be omitted from the returned data frame (their absence denoting missingness). Otherwise, missing values will be included as NA's.

series.id

A factor (or variable coercible to factor) of the same length as response, indicating the series to which each observation belongs.

timestamps

A variable of the same length as response, indicating the time period to which each observation belongs.

Value

LongToWide returns a zoo matrix with the time series in wide format. WideToLong returns a 3-column data frame with columns "time", "series", and "values".

Examples

Run this code
# NOT RUN {
data(gdp)
gdp.wide <- LongToWide(gdp$GDP, gdp$Country, gdp$Time)
gdp.long <- WideToLong(gdp.wide)
# }

Run the code above in your browser using DataLab