【发布时间】:2018-03-13 16:48:35
【问题描述】:
已编辑:根据用户 @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
【问题讨论】:
-
您的内核平滑代码似乎与您的要求无关。您能否通过仅发布来自
all_list的数据(复制和粘贴dput(all_list)的输出)、您尝试 将all_list存储到矩阵中的代码以及预期输出(dput(mat_list))? -
@user - 公平点。我已经实施了你的建议。你能帮忙吗?