繁体   English   中英

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

[英]Using purrr to convert list of vectors to list of matrices

编辑:根据用户@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

这是使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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