Question

I'm trying to resolve two main issues on an R program.

First I need to create a kind-of matrix structure to store for different individuals (rows) the values of different variables (columns).

The main problem is that in each pair row-column I need to have a vector of observations in the form of a time-series. Because the time series in each different variable aren't sampled at sequential times the different time series aren't regular.

I'm thinking of using the rownames and colnames as keys for accessing the items in the matrix.

Don't know if this is the best structure for what I need. Was using dataframes but had the suggestion to move to matrix because it should store a bit of big amounts of data (datastream).

Was it helpful?

Solution

You can store individual time series in a list, say l, and set dim attribute on it. You can set dimnames (i.e. row names and column names) too. With that you can use it almost as if it is a matrix/data.frame

# Generate length 15 vectors for 10 subjects
l <- replicate(10, list(rnorm(15)))
dim(l) <- c(5, 2)
dimnames(l) <- list(subject=1:5, variable=c("a", "b"))
l
##        variable
## subject a          b         
##       1 Numeric,15 Numeric,15
##       2 Numeric,15 Numeric,15
##       3 Numeric,15 Numeric,15
##       4 Numeric,15 Numeric,15
##       5 Numeric,15 Numeric,15

Now you can:

l[[1,1]]    # time series for subject 1, var 1
##  [1] -0.02425  0.88986  0.36260 -1.78774 -1.48874 -1.46750  0.38329
##  [8]  0.18573 -1.65675  0.59374  0.81669  1.06867 -1.71847  0.81889
## [15]  0.10796

or

l[[2, "b"]] # time series for subject 2, var "b"
##  [1]  0.45616 -0.67563 -1.42116 -0.42621  0.51648  0.35147  0.68243
##  [8]  1.17581 -0.16696  0.77492 -1.76446  1.50580  0.06075  0.37734
## [15] -0.92797

etc...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top