Hi,
I was trying to gather/combine the results returned from the mclapply function.
This is how I do it if only one data object is returned:
#===== This works fine ======
library(multicore)
frandom1 <- function(iter,var1 = 3,var2 =2){
mat <- matrix(rnorm(var1*var2),nrow=var1,ncol=var2)
return(mat)
}
N <- 3
### OK
temp1 <- mclapply(1:N,frandom1,mc.cores=2)
result1 <- do.call(rbind,temp1)
print(result1)
#=======================
Now, I want to return more than one object from my function and I am not sure
how best to combine the values returned. My current code is:
#======= ??? ====frandom2 <- function(iter,var1 = 3,var2 =2){
mat <- matrix(rnorm(var1*var2),nrow=var1,ncol=var2)
vect1 <- sample(1:1000,var1)
return(list(mat,vect1))
}
temp2 <- mclapply(1:N,frandom2,mc.cores=2)
#### Combining returned values????
result2 <- result3 <- c()
for(k in 1:N){
thismat <- temp2[[k]][[1]]
result2 <- rbind(result2,thismat)
thisvect <- temp2[[k]][[2]]
result3 <- c(result3,thisvect)
}
#=============
Is there a more elegant way of combining these values (as in the top example)?
Although this works, for a large value of N ( N > 500,000), running a loop
would be very expensive.
Any help would be appreciated!
thanks!
[[alternative HTML version deleted]]