将列表的多个元素转换为矩阵R.

时间:2016-05-07 05:08:44

标签: r list matrix

我有一个列表,例如:

    [[1]]
              [,1]
[1,] -1.775291e-04
[2,] -1.267184e-04
[3,] -1.573868e-03
[4,]  4.157234e-02
[5,] -4.864003e-02
[6,]  2.316697e-05

[[2]]
              [,1]
[1,] -0.0010882973
[2,]  0.0009780598
[3,]  0.0003006506
[4,]  0.1579244926
[5,]  0.1655930418
[6,] -0.0006471336

[[3]]
              [,1]
[1,]  2.861335e-03
[2,] -3.259585e-05
[3,]  3.377353e-03
[4,]  1.224368e-02
[5,]  6.205352e-02
[6,] -3.028701e-04

[[4]]
              [,1]
[1,]  0.0023484525
[2,] -0.0007958971
[3,]  0.0038275408
[4,] -0.1705923272
[5,] -0.0706761005
[6,] -0.0004604092

我想改变它,以便我有一个矩阵,每个列表成为数据表的第一行,基本上我想转置列表中的每个元素,然后将它们放在另一个上面,我以后可以处理它们。

2 个答案:

答案 0 :(得分:2)

如果d是您的列表:

d = list(matrix(rnorm(6), ncol = 1), matrix(rnorm(6), ncol = 1),matrix(rnorm(6), ncol = 1),matrix(rnorm(6), ncol = 1))

然后就这样使用:

t(sapply(d, c))

将产生4x6矩阵。

            [,1]       [,2]        [,3]       [,4]        [,5]       [,6]
[1,] -0.02910676 -0.8722619 -1.48340110 -1.9914850  0.80751174 -1.1062207
[2,] -0.38604263  0.6417695  0.02404823 -0.3484978 -1.03931644  1.0919702
[3,]  0.19229699  0.3389690  1.68451808  0.7688967  0.01010725 -0.3203104
[4,]  0.36910577 -0.4922259  0.81362335 -1.9770308  0.65197010  0.2063001

答案 1 :(得分:1)

如果L是列表,那么:

t(simplify2array(L))

没有使用任何包裹。