-2

I would like to create a separate dataframe from list of data elements of matrices. For example, I have the following list of matrix elements:-

> A[[1]]
$`up`
,, T+1
     [,1] [,2] [,3]
[1,]    2    4    3
[2,]    1    5    7

$`down`
,, T+1
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    2    4    2

$`right`
,,T+1
     [,1] [,2] [,3]
[1,]    5    6    7
[2,]    9    2    3

Suppose that I want to create a separate list of data elements. I don't want the element names '$'up'/$'down'/$'right' to appear on my output. With the current code, every time I want to call the first matrix, I have to write the code as

A[[1]]$'up'[,,1] or A[[1]]$'down'[,,1] or A[[1]]$'right'[,,1]

Is it possible to create a separate list so that whenever I want to call it, it would be simpler without the elements name. For example, I just want to call A[[1]], whenever I want to call the first matrix, A[[2]] for the second matrix and so on. It will look something like this:-

> A[[1]]
     [,1] [,2] [,3]
[1,]    2    4    3
[2,]    1    5    7

> A[[2]]
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    2    4    2

> A[[3]]
     [,1] [,2] [,3]
[1,]    5    6    7
[2,]    9    2    3
NSAA
  • 175
  • 1
  • 3
  • 14

1 Answers1

1

With your definition of A you can already access the i-th matrix via A[[i]], e.g.:

> A[[1]]
     [,1] [,2] [,3]
[1,]    2    4    3
[2,]    1    5    7

Alternatively you can create a 3-dimensional array, where the third dimension indicates the number of the matrix:

B <- array(do.call("cbind", A), c(2,3,4))

Now you can access the i-th matrix (i-th z-slice of the "cube") via B[,,i], e.g.:

> B[,,2]
     [,1] [,2] [,3]
[1,]    3    2    1
[2,]    2    4    2
Patrick Roocks
  • 3,129
  • 3
  • 14
  • 28