簡體   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