使用purrr将向量列表转换为矩阵列表

时间:2018-03-13 16:48:36

标签: r tidyverse purrr

已编辑:根据用户@useR的建议,我有以下reprex 我所要求的问题(见帖子的结尾)。


# This is the source list i.e. list of vectors
all_list <- list(c(1, 10, 19, 28, 37)
                 , c(4, 13, 22, 31, 40)
                 , c(7, 16, 25, 34, 43)
                 , c(2, 11, 20, 29, 38)
                 , c(5, 14, 23, 32, 41)
                 , c(8, 17, 26, 35, 44)
                 , c(3, 12, 21, 30, 39)
                 , c(6, 15, 24, 33, 42)
                 , c(9, 18, 27, 36, 45))
all_list
#> [[1]]
#> [1]  1 10 19 28 37
#> 
#> [[2]]
#> [1]  4 13 22 31 40
#> 
#> [[3]]
#> [1]  7 16 25 34 43
#> 
#> [[4]]
#> [1]  2 11 20 29 38
#> 
#> [[5]]
#> [1]  5 14 23 32 41
#> 
#> [[6]]
#> [1]  8 17 26 35 44
#> 
#> [[7]]
#> [1]  3 12 21 30 39
#> 
#> [[8]]
#> [1]  6 15 24 33 42
#> 
#> [[9]]
#> [1]  9 18 27 36 45

# Create an empty list of matrices
# We want to put all_list in here so that it looks like mat_list
empt_mat <- list(structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), 
                 structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), 
                 structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), 
                 structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), 
                 structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)))
empt_mat
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> [[3]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> [[4]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> [[5]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0

# As a check - all_list once put in empt_mat, should lead to empt_mat
# looking like mat_list
mat_list <- list(structure(1:9, .Dim = c(3L, 3L))
                 , structure(10:18, .Dim = c(3L, 3L))
                 , structure(19:27, .Dim = c(3L, 3L))
                 , structure(28:36, .Dim = c(3L, 3L))
                 , structure(37:45, .Dim = c(3L, 3L)))
mat_list
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]   10   13   16
#> [2,]   11   14   17
#> [3,]   12   15   18
#> 
#> [[3]]
#>      [,1] [,2] [,3]
#> [1,]   19   22   25
#> [2,]   20   23   26
#> [3,]   21   24   27
#> 
#> [[4]]
#>      [,1] [,2] [,3]
#> [1,]   28   31   34
#> [2,]   29   32   35
#> [3,]   30   33   36
#> 
#> [[5]]
#>      [,1] [,2] [,3]
#> [1,]   37   40   43
#> [2,]   38   41   44
#> [3,]   39   42   45

有人可以说明如何优雅地使用purrr(或tidyverse工具)将all_list放入empt_mat,以便empt_mat看起来像mat_list一次这些值放在哪里?

真的想避免繁琐的循环,以便实现这一点,因此偏好purrr

1 个答案:

答案 0 :(得分:0)

以下是使用do.callmap的解决方案:

library(tidyverse)

all_list %>%
  do.call(rbind, .) %>%
  data.frame() %>%
  map(~ matrix(., 3,3, byrow = TRUE)) %>%
  unname()

我没有创建empt_mat并尝试在其中存储all_list,而是将all_list转换为与mat_list具有相同格式。

<强>结果:

[[1]]
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

[[2]]
     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

[[3]]
     [,1] [,2] [,3]
[1,]   19   22   25
[2,]   20   23   26
[3,]   21   24   27

[[4]]
     [,1] [,2] [,3]
[1,]   28   31   34
[2,]   29   32   35
[3,]   30   33   36

[[5]]
     [,1] [,2] [,3]
[1,]   37   40   43
[2,]   38   41   44
[3,]   39   42   45