title: In R how to create big matrix in parallel
body: <p>Now I have a big matrix to build. It is very time-consuming. So I want to do it in parallel. For example, the matrix is 10^7 x 16, for each column I define a vector function to calculate the values. Like here, I should have 16 functions. Then, I want to assign the jobs to 16 cores to compute each column in parallel and finally give me this big matrix. How can I do that in R? Thanks!</p>
  
  <p>Now I tried serval packages like parallel, foreach, doParallel. It doesn't work for some errors. </p>
  
  <p>Here is a simple example I tried</p>
  
  <pre><code>library(parallel)
  
  
  N1 = 100
  
  
  rka = rep(1, N1)
  rkb = rep(2, N1)
  rkab = rep(3,N1)
  rna = rep(4,N1)
  rnb = rep(5,N1)
  rnab = rep(6,N1)
  
  E1 = cbind(rna,rnb-1,rnab,rka-1,rkb,rkab+1) 
  E2 = cbind(rna,rnb,rnab,rka-1,rkb,rkab+1)   
  E3 = cbind(rna-1,rnb,rnab,rka,rkb-1,rkab+1) 
  E4 = cbind(rna,rnb,rnab,rka,rkb-1,rkab+1) 
  
  VDelta = Matrix(0,nrow= 100,ncol= 4,sparse=TRUE)
  
  
   N_a&lt;- cbind(E1[,1]+E1[,3]+E1[,4]+E1[,6],E2[,1]+E2[,3]+E2[,4]+E2[,6],E3[,1]+E3[,3]+E3[,4]+E3[,6],E4[,1]+E4[,3]+E4[,4]+E4[,6])
  N_b&lt;- cbind(E1[,2]+E1[,3]+E1[,5]+E1[,6],E2[,2]+E2[,3]+E2[,5]+E2[,6],E3[,2]+E3[,3]+E3[,5]+E3[,6],E4[,2]+E4[,3]+E4[,5]+E4[,6])
  
  Mab &lt;- M_0*(1-N_b/K_b)
  Mba &lt;- M_0*(1-N_a/K_a)
  
  
  lambda_ab&lt;- 2/(Mab+Mba)
  C_b = 0.4
  C_a = 0.4
  
  fun = function(i){
    if(i == 2) VDelta[,2] &lt;- C_b*(E2[,6] +1)
    else if (i == 4) VDelta[,4] &lt;- C_a*(E4[,6] +1)
    else if (i == 1) VDelta[,1] &lt;- lambda_ab[,1]*E1[,6]
    else VDelta[,3] &lt;- lambda_ab[,3]*E3[,6]
  
    VDelta[,i] = VDelta[,i]
  }  
  
  no_cores = detectCores()
  cl&lt;-makeCluster(no_cores)
  clusterExport(cl, "fun")
  clusterExport(cl, "lambda_na")
  clusterExport(cl, "lambda_nb")
  clusterExport(cl, "Mab")
  clusterExport(cl, "Mba")
  clusterExport(cl, "lambda_ab")
  clusterExport(cl, "E1")
  clusterExport(cl, "E2")
  clusterExport(cl, "E3")
  clusterExport(cl, "E4")
  clusterExport(cl, "C_b")
  clusterExport(cl, "C_a")
  clusterExport(cl, "VDelta")
  V = parSapply(cl = cl, 1:4,fun)
  stopCluster(cl)
  </code></pre>
  
  <p>Then, I got this error :</p>
  
  <pre><code>Error in checkForRemoteErrors(val) : 
    4 nodes produced errors; first error: object of type 'S4' is not subsettable
  </code></pre>