0

I want to save multiple classes in a list within a foreach loop in R. I have a reproducible example here.

cl <- makeCluster(4)
registerDoParallel( cl)
start_time <- Sys.time()

acomb <- function(...) abind(..., along=3)
results <-foreach(i = 
1:10,.combine="acomb",.multicombine=TRUE,
  .packages=c('doParallel','foreach','base','abind')) %dopar% {
a = array(data=sample(rnorm(100000, mean=i, sd=1),9000),dim=c(3,3,1000))
aperm(a,c(2,1,3))
}
stopCluster(cl)
end_time <- Sys.time()
end_time - start_time

This gives me an output of 3*3*10000 array.

I got the result as an array which is fine, however, I want their sum and mean of each as well stored as a vector in a list. The final result should be something like this:

list(data=results,mean=mean,sum=sum).

Where data should be 3*3*10000, mean should be a vector of 10000 elements and sum should also be a vector of 10000 elements. I want this to be with in foreach loop.

I also searched it a bit and found saving multiple outputs in foreach. However, I could not modify it to what I need here.

squancy
  • 565
  • 1
  • 7
  • 25
user3978632
  • 283
  • 4
  • 17

1 Answers1

1

Return a list in foreach and don't combine directly:

results2 <- foreach(i = 1:10, .packages=c('doParallel','foreach','base','abind')) %dopar% {
                    a = array(data=sample(rnorm(100000, mean=i, sd=1),9000),dim=c(3,3,1000))
                    res <- aperm(a,c(2,1,3))
                    sum <- apply(res, 3, sum)
                    list(data = res, sum = sum, mean = sum / prod(dim(res)[-3]))
                  }

Then, combine:

library(purrr)
final <- list(data = do.call(acomb, map(results2, "data")), 
              sum  = do.call(c, map(results2, "sum")), 
              mean = do.call(c, map(results2, "mean")))
F. Privé
  • 11,423
  • 2
  • 27
  • 78