I am currently turning a for
loop into a foreach
loop so I can parallelize the calculations of the inner loop. I am able to do that and get the correct output. However, I want the format of the output from foreach
to be equal to the format I had in my old for
loop. Here's a reproducible example to what I want to do:
FOR LOOP:
x <- c(1:10)
y <- c(5)
z <- c(3)
result_list <- list()
for(i in length(x)){
for(j in 1:y){
for(k in 1:z){
result <- rnorm(n = 250)
result_list[[paste(i, j, k)]] <- result
}
}
}
That's the output I get and what I want to achieve:
library(dplyr)
glimpse(result_list)
List of 15
$ 10 1 1: num [1:250] -0.382 -0.156 -0.747 1.139 -1.824 ...
$ 10 1 2: num [1:250] -0.181 0.526 0.255 -0.369 -1.517 ...
$ 10 1 3: num [1:250] 0.3621 1.3634 -0.0507 -0.3943 -1.2183 ...
$ 10 2 1: num [1:250] -1.453 0.731 2.761 -0.586 -1.631 ...
$ 10 2 2: num [1:250] -0.663 -0.641 -1.852 1.58 0.133 ...
$ 10 2 3: num [1:250] -1.334 0.803 -0.116 0.618 -1.339 ...
$ 10 3 1: num [1:250] 0.158 -1.296 -0.947 -0.515 -0.208 ...
$ 10 3 2: num [1:250] -0.604 1.956 0.127 1.846 -0.549 ...
$ 10 3 3: num [1:250] 0.365 -0.467 -0.589 -1.251 0.491 ...
$ 10 4 1: num [1:250] -1.138 0.883 1 0.729 -1.566 ...
$ 10 4 2: num [1:250] -0.0461 2.3096 -1.5347 0.3722 0.3125 ...
$ 10 4 3: num [1:250] 0.127 -0.728 0.402 1.783 -1.457 ...
$ 10 5 1: num [1:250] 1.855 2.224 1.301 0.166 -0.28 ...
$ 10 5 2: num [1:250] 0.463 -1.011 1.067 -1.305 -0.51 ...
$ 10 5 3: num [1:250] 1.937 0.651 -0.424 -0.714 -0.225 ...
That's the code to the FOREACH LOOP:
library(foreach)
result_list <- list()
resultado_foreach <- foreach::foreach(i = 1:10, .inorder = TRUE) %do% {
foreach::foreach(j = 1:5, .inorder = TRUE) %do% {
foreach::foreach((k = 1:3)) %dopar% {
result <- rnorm(n = 250)
result_list[[paste(i, j, k)]] <- result
}
}
}
Although, I get a list of lists of lists (a nested-nested list):
glimpse(resultado_foreach)
List of 10
$ :List of 5
..$ :List of 3
.. ..$ : num [1:250] 0.911 0.594 -0.453 0.651 2.303 ...
.. ..$ : num [1:250] -0.664 -0.696 0.741 -2.78 -0.992 ...
.. ..$ : num [1:250] -0.6877 -1.1266 -1.7784 0.473 0.0185 ...
..$ :List of 3
.. ..$ : num [1:250] 1.273 0.129 0.902 2.47 0.177 ...
.. ..$ : num [1:250] 0.705 0.519 1.219 -1.682 -0.355 ...
.. ..$ : num [1:250] 1.138 0.422 -1.025 -0.237 0.418 ...
..$ :List of 3
.. ..$ : num [1:250] -1.636 -1.297 -1.115 -0.138 0.174 ...
.. ..$ : num [1:250] 0.56 -1.311 0.641 0.861 -0.601 ...
.. ..$ : num [1:250] 0.198 -1.197 0.781 -0.571 -0.141 ...
..$ :List of 3
.. ..$ : num [1:250] -0.355 -0.649 -1.046 -0.717 -0.97 ...
.. ..$ : num [1:250] -1.086 0.912 -0.996 0.303 1.418 ...
.. ..$ : num [1:250] 0.8827 -0.0761 1.3504 -0.5301 0.2267 ...
..$ :List of 3
.. ..$ : num [1:250] -1.826 1.286 1.585 -0.359 -0.955 ...
.. ..$ : num [1:250] -0.588 -0.314 -0.223 -0.779 0.569 ...
.. ..$ : num [1:250] 1.047 -0.242 -0.345 0.27 -0.158 ...
The foreach
output is much longer than what I have put here.
I have already tried many combinations and set many functions to the .combine
argument in the foreach
function. So, how can I obtain the output in the same format of the for loop
?
Currently in your for loop you have
for(i in length(x)) {...}
but length(x)
is 10 so you are just doing for(i in 10)
which only loops once. That's not the same as for (i in 1:10)
. A safer alternative is for (i in seq_along(x))
.
So let's say that you expect 10 outer loops, each with 5 middle loops, and each with 3 inner loops. That should be 10x5x3 = 150 total results.
If you want to use nested loops with for each, you should use the %:%
operator. Plus you'll also want to use .combine=c
to concatenate the inner loops into a single list at the end. These options are discussed on the ?foreach
help page.
A better version would be
resultado_foreach <-
foreach::foreach(i = 1:10, .inorder = TRUE, .combine=c) %:%
foreach::foreach(j = 1:5, .inorder = TRUE, .combine=c) %:%
foreach::foreach((k = 1:3)) %dopar% {
rnorm(n = 250)
}
This will return a list of length 150 each with a vector of length 250.
Note that you shouldn't modify global variables when using foreach
. Each block should return a value and foreach
will collect and combine those values for you.