I have vectors with latitudes and longitudes:
longDim
[1] -79.65770 -79.21761 -78.77750
latiDim
[1] -39.70588 -39.26471 -38.82353
and I wanted to loop over their combination in parallel. To do so, first I used expand.grid
to create a data frame with all of their possible combinations:
my.grid <- expand.grid(longDim, latiDim)
and then I used mclapply()
on the rows of the resulting data frame:
mclapply(1:nrow(my.grid), function(x){some_function})
where some_function
returns a list with two objects, each with length 139.
Therefore, as a result I got a nested list with dimensions 9x2 that looks like this:
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" ...
Now, I need to reshape this list from 9x2 to the 3x3x2 dimension again. This is the format I am looking for:
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" ...
How can I achieve that?
Code to reproduce l1
and l2
can be found at: https://pastebin.com/raw/LTyZi0mp (too long to post it here)
We can also use gl
to create a grouping index for split
ing
split(lst, as.integer(gl(length(lst1), 3, length(lst1))))
lst1 <- replicate(9, list(list(x = 1:5, y = 1:5)))