简体   繁体   中英

Using purrr to convert list of vectors to list of matrices

EDITED: Based on suggestion by user @useR I have the following reprex for my required question (see end of post).


# 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

Could anyone please show how to elegantly use purrr (or tidyverse tools) to put all_list into empt_mat so that empt_mat will look like mat_list once the values are put in?

Really want to avoid cumbersome loops so that this is achieved, hence the preference for purrr

Here is a solution using do.call and map :

library(tidyverse)

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

Instead of creating empt_mat and trying to store all_list in it, I transformed all_list to have the same format as mat_list .

Result:

[[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

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