lapply 创建的重塑列表
Reshape list created by lapply
我有带有纬度和经度的向量:
longDim
[1] -79.65770 -79.21761 -78.77750
latiDim
[1] -39.70588 -39.26471 -38.82353
我想并行遍历它们的组合。为此,我首先使用 expand.grid
创建一个包含所有可能组合的数据框:
my.grid <- expand.grid(longDim, latiDim)
然后我在结果数据框的行上使用了mclapply()
:
mclapply(1:nrow(my.grid), function(x){some_function})
其中 some_function
returns 包含两个对象的列表,每个对象的长度为 139。
因此,我得到了一个尺寸为 9x2 的嵌套列表,如下所示:
str(l1)
List of 9
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
现在,我需要再次将此列表从 9x2 维度重新调整为 3x3x2 维度。这是我正在寻找的格式:
str(l2)
List of 3
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
我怎样才能做到这一点?
可在以下位置找到重现 l1
和 l2
的代码:https://pastebin.com/raw/LTyZi0mp(这里 post 太长了)
您可以使用 split()
.
split(lst, cut(1:length(lst), 3, labels = FALSE))
测试
lst <- replicate(9, list(list(x = 1:5, y = 1:5)))
result <- split(lst, cut(seq_along(lst), 3, labels = FALSE))
str(result)
# List of 3
# $ 1:List of 3
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# etc.
我们也可以使用gl
为split
ing
创建分组索引
split(lst, as.integer(gl(length(lst1), 3, length(lst1))))
数据
lst1 <- replicate(9, list(list(x = 1:5, y = 1:5)))
我有带有纬度和经度的向量:
longDim
[1] -79.65770 -79.21761 -78.77750
latiDim
[1] -39.70588 -39.26471 -38.82353
我想并行遍历它们的组合。为此,我首先使用 expand.grid
创建一个包含所有可能组合的数据框:
my.grid <- expand.grid(longDim, latiDim)
然后我在结果数据框的行上使用了mclapply()
:
mclapply(1:nrow(my.grid), function(x){some_function})
其中 some_function
returns 包含两个对象的列表,每个对象的长度为 139。
因此,我得到了一个尺寸为 9x2 的嵌套列表,如下所示:
str(l1)
List of 9
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 2
..$ su.25: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ su.30: Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
现在,我需要再次将此列表从 9x2 维度重新调整为 3x3x2 维度。这是我正在寻找的格式:
str(l2)
List of 3
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
$ :List of 3
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
..$ :List of 2
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
.. ..$ : Named num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "names")= chr [1:139] "1961" "1962" "1963" "1964" ...
我怎样才能做到这一点?
可在以下位置找到重现 l1
和 l2
的代码:https://pastebin.com/raw/LTyZi0mp(这里 post 太长了)
您可以使用 split()
.
split(lst, cut(1:length(lst), 3, labels = FALSE))
测试
lst <- replicate(9, list(list(x = 1:5, y = 1:5)))
result <- split(lst, cut(seq_along(lst), 3, labels = FALSE))
str(result)
# List of 3
# $ 1:List of 3
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# ..$ :List of 2
# .. ..$ x: int [1:5] 1 2 3 4 5
# .. ..$ y: int [1:5] 1 2 3 4 5
# etc.
我们也可以使用gl
为split
ing
split(lst, as.integer(gl(length(lst1), 3, length(lst1))))
数据
lst1 <- replicate(9, list(list(x = 1:5, y = 1:5)))