Roland
Roland

Reputation: 537

How to store a matrix-valued time-series in R?

I'm wondering what's the best option to store (and process) a multivariate (in particular matrixvalued) time-series in R.

I have a large data frame which stores all the data as well as the time variable (in this case named year, as a column.)

Here's what I could think of, but both options have their disadvantages:

  1. A list of dataframes, e.g. via my.list[[i]] <- my.df[,year==i], looping over i=2008:2011. However, I don't know how to extract, e.g. the univariate series of the entry in the upper left corner of all these list entries.

  2. As a ts object, but this would require transforming the matrix for a given year into a row vector, which might be a bit unwieldy to manipulate.

Are there other, better ways to store a time series of matrices (or even data frames)?

Upvotes: 0

Views: 1969

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368251

You do want the zoo or xts packages which have a million examples here and on the web, plus a lot of excellent documentation starting from the package vignettes.

Both zoo and xts essentially wrap a single index column of dates or datetimes around a (numeric) matrix of values. You can still do all you can otherwise do with a matrix, plus better / smarter indexing and merging and subsetting etc pp.

Look for example here under a query [r] zoo xts in the search box above. This (currently) gets me 509 results many of which will be worked examples.

Upvotes: 2

Related Questions