Parallelize nested for-loop on 3 dimensional array in R
<p>Using R on a Windows machine, I am currently running a nested loop on a 3D array (720x360x1368) which cycles through d1 and d2 to apply a function over d3 and assemble the output to a new array of similar dimensionality.</p>

<p>In the following reproducible example, I have reduced the dimensions by factor 10, to make execution faster. </p>

<pre><code>library(SPEI)

old.array = array(abs(rnorm(50)), dim=c(72,36,136))

new.array = array(dim=c(72,36,136))

for (i in 1:72) {
  for (j in 1:36) {
    new.listoflists &lt;- spi(ts(old.array[i,j,], freq=12, start=c(1901,1)), 1, na.rm = T)
    new.array[i,j,] = new.listoflists$fitted
  }
}
</code></pre>

<p>where spi() is a function from the SPEI package returning a list of lists, of which one particular list <code>$fitted</code>of length 1368 is used from each loop increment to cunstruct the new array.</p>

<p>While this loop works flawlessly, it takes quite a long time to compute. I have read that <code>foreach</code>can be used to parallelize <code>for</code> loops.</p>

<p>However, I do not understand how the nesting and the assembling of the new array can be achieved such that the dimnames of the old and the new array are consistent.</p>

<p><em>(In the end, what I want to be able to, is to transform both the old and the new array into a "flat" long panel data frame using <code>as.data.frame.table()</code> and merge them along their three dimensions.)</em></p>

<p>Any help on how I can achieve the desired output using parallel computing will be highly appreciated!</p>

<p>Cheers<br>
CubicTom</p>