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.