[R-sig-hpc] nested foreach loop

aisha at mun.ca aisha at mun.ca
Thu Feb 4 16:12:25 CET 2016


Hello,

When I run this simple example of nested foreach loop:

 foreach(i=1:3, .packages="foreach") %dopar% {
+ foreach(j=1:3) %dopar% {
+ y=sqrt(i+j)
+ }
+ }
I get

[[1]]
[[1]][[1]]
[1] 1.414214

[[1]][[2]]
[1] 1.732051

[[1]][[3]]
[1] 2


[[2]]
[[2]][[1]]
[1] 1.732051

[[2]][[2]]
[1] 2

[[2]][[3]]
[1] 2.236068

[[3]]
[[3]][[1]]
[1] 2

[[3]][[2]]
[1] 2.236068

[[3]][[3]]
[1] 2.44949


> y
[1] 1.000000 1.414214 1.732051

Why y gives [1] 1.000000 1.414214 1.732051
1.000000 is not even a correct value for y=sqrt(i+j)
and why y is a vector in the first place? 
Thanks,

Ibrahim



More information about the R-sig-hpc mailing list