Let's say I want to use foreach
in the doParallel
package to return a list of two data frames of different dimensions like the following:
a<-NULL
b<-NULL
for(i in 1:100){
a<-rbind(a,data.frame(input=i,output=i/2))
if(i > 5){
b<-rbind(b,data.frame(input=i,output=i^2))
}
}
list(a,b)
Sinceforeach
returns an object, there's no (at least to me) obvious way to do the above with foreach
.
NOTE: this is a much simplified version of the problem I'm actually working with so solving the problem by using lapply (or something along those lines) won't work. The spirit of my question is how to do this with foreach
.
I figured it out. You have to define your own function that combines the lists in exactly the way you want.
#takes an arbitrary number of lists x all of which much have the same structure
comb <- function(x, ...) {
mapply(rbind,x,...,SIMPLIFY=FALSE)
}
foreach(i=1:10, .combine='comb') %dopar% {
a<-rbind(a,data.frame(input=i,output=i/2))
if(i > 5){
b<-rbind(b,data.frame(input=i,output=i^2))
}
list(a,b)
}