0

I am new to R. I've written this very simple script to highlight my problem. If I run this regular for loop testdata is updated on each iteration just as I want it.

a = 5
b = 4
c = 3
testdata = matrix(nrow=100, ncol=5)
for(j in 1:100){
testdata[j,1] <- a*j
testdata[j,2] <- b*j
testdata[j,3] <- c*j
testdata[j,4] <- (a+b)*j
testdata[j,5] <- (a+c)*j
}

However this parallel version using foreach completes the computations but they are not updated in testdata.

a = 5
b = 4
c = 3
testdata = matrix(nrow=100, ncol=5)
library(foreach)
library(doParallel)
library(doMC)
registerDoMC()
getDoParWorkers()  # Checking the number of cores. 

foreach(j = 1:100) %dopar% {
  testdata[j,1] <- a*j
  testdata[j,2] <- b*j
  testdata[j,3] <- c*j
  testdata[j,4] <- (a+b)*j
  testdata[j,5] <- (a+c)*j
}

I have attempted to follow examples here and elsewhere on the internet but most of the examples were too deep in R shoptalk and I couldn't follow. How can I make this parallel version do what the non-parallel version does. Thanks.

Eddy
  • 135
  • 7

1 Answers1

0

You should check out the docs for the foreach package. In the foreach(j = 100) section of your code, you can specify the argument .combine to tell foreach how to compile your results. Since you want a 5x100 data frame/matrix, you would logically write a vector of your five arguments (i.e. c(a*j, b*j, c*j, (a+b)*j, (a+c)*j)), and rbind them to make a single data frame. Check out my code below:

a = 5
b = 4
c = 3

library(foreach)
library(doParallel) 
library(parallel)

## Assuming you want to use all of your cores
registerDoParallel(cores = detectCores())

## Specify your .combine argument below
foreach(j = 1:100, .combine = "rbind") %dopar% {
    c(a*j, b*j, c*j, (a+b)*j, (a+c)*j)
}

And this spits out:

           [,1] [,2] [,3] [,4] [,5]
result.1      5    4    3    9    8
result.2     10    8    6   18   16
result.3     15   12    9   27   24
result.4     20   16   12   36   32
result.5     25   20   15   45   40
...

You could then take this one step further by assigning this to the variable you want:

...
testdata <- foreach(j = 1:100, .combine = "rbind") %dopar% {
                c(a*j, b*j, c*j, (a+b)*j, (a+c)*j)
            }
testdata <- as.data.frame(testdata, row.names = FALSE)

Hope this helps!