简体   繁体   中英

How to turn list into an array in R?

I want to turn a list into an array as follows in R. Each element in data happens to be a 3-D array but the class is "list" whereas the desired output is class "array."

Output is class list:

>data
  $ind1
            [,1]        [,2]     [,3
   [1,] 57.71157  1.95974925 269.881
   [2,] 68.61496 -3.63548833 269.836
   [3,] 70.55135  0.31777057 266.963

  $ind2
            [,1]         [,2]     [,
   [1,] 61.31171  3.733692169 274.02
   [2,] 65.56570 -5.445514560 271.48
   [3,] 68.91904 -3.202808022 269.16

  $ind3
            [,1]       [,2]     [,3]
   [1,] 57.40768  -3.343789 253.6673
   [2,] 62.94523 -13.065327 253.8842
   [3,] 66.52199 -12.943937 250.7863

Desired output is class array:

>data$ind
  , , ind1

            [,1]        [,2]     [,3]
   [1,] 57.71157  1.95974925 269.8813
   [2,] 68.61496 -3.63548833 269.8362
   [3,] 70.55135  0.31777057 266.9638

  , , ind2
            [,1]         [,2]     [,3]
   [1,] 61.31171  3.733692169 274.0263
   [2,] 65.56570 -5.445514560 271.4852
   [3,] 68.91904 -3.202808022 269.1676

  , , ind3
            [,1]       [,2]     [,3]
   [1,] 57.40768  -3.343789 253.6673
   [2,] 62.94523 -13.065327 253.8842
   [3,] 66.52199 -12.943937 250.7863

What about:

 L <- list(matrix(rnorm(2), nrow = 4), matrix(rnorm(2), nrow = 4))
> L
[[1]]
          [,1]
[1,] 0.1411404
[2,] 1.0566137
[3,] 0.1411404
[4,] 1.0566137

[[2]]
           [,1]
[1,] -0.6792944
[2,] -1.2507825
[3,] -0.6792944
[4,] -1.2507825

# two ways here
array(unlist(L), dim = c(nrow(L[[1]]), ncol(L[[1]]), length(L)))
array(unlist(L), c(dim(L[[1]]), length(L))) # Thanks to David Arenburg in the comments
, , 1

          [,1]
[1,] 0.1411404
[2,] 1.0566137
[3,] 0.1411404
[4,] 1.0566137

, , 2

           [,1]
[1,] -0.6792944
[2,] -1.2507825
[3,] -0.6792944
[4,] -1.2507825

> 

Easiest way is the abind package:

library(abind)

test_mat <- lapply(1:3, function(x) matrix(runif(12), nrow=3, ncol=4))

abind(test_mat, along=3)

, , 1

          [,1]      [,2]      [,3]      [,4]
[1,] 0.9493937 0.7062080 0.2965067 0.4881118
[2,] 0.1280332 0.2595627 0.6954514 0.7917190
[3,] 0.3611595 0.2219669 0.7363357 0.3025325

, , 2

          [,1]       [,2]      [,3]      [,4]
[1,] 0.5407829 0.70890818 0.7959612 0.4637777
[2,] 0.7743566 0.07994314 0.2751361 0.7770016
[3,] 0.5873859 0.55288233 0.5701440 0.5282996

, , 3

           [,1]      [,2]      [,3]       [,4]
[1,] 0.34326044 0.6389373 0.7563659 0.83533279
[2,] 0.06685802 0.7410194 0.1814166 0.91156363
[3,] 0.86907376 0.4374175 0.1335296 0.02717944

You can use simplify2array to turn a list into an array .

simplify2array(L)
#, , i1
#
#     [,1] [,2]
#[1,]    1    2
#
#, , i2
#
#     [,1] [,2]
#[1,]    3    4
#
#, , i3
#
#     [,1] [,2]
#[1,]    5    6

Data:

L <- list(i1 = matrix(1:2, 1), i2 = matrix(3:4, 1), i3 = matrix(5:6, 1))
L
#$i1
#     [,1] [,2]
#[1,]    1    2
#
#$i2
#     [,1] [,2]
#[1,]    3    4
#
#$i3
#     [,1] [,2]
#[1,]    5    6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM