title: Save results of foreach loop in a matrix
body: <p>I am trying to adapt a for loop to use it in parallel as a foreach loop. For the for loop I create an empty matrix then fill it with the values generated by the for loop. But this approach doesn't work in the foreach loop, </p>
  
  <pre><code>results&lt;-matrix(nrow=length(classes),ncol=length(files))
  dimnames(results)[[1]]&lt;-classes
  dimnames(results)[[2]]&lt;-files
  
  ptime&lt;-system.time({
      foreach(z = 1:length(files),.packages="raster") %dopar% {
        raster &lt;- raster(paste(folder,files[z],sep=""))
        data&lt;-getValues(raster)
        clp &lt;- na.omit(data)
        for(i in 1:length(classes)){
          results[i,z]&lt;-length(clp[clp==classes[i]])/length(clp)
          print(z)
        }
      }
  })
  </code></pre>
  
  <p>My results matrix is still just filled with na's, it doesn't get populated. </p>
  
  <p>z is a raster file, i is a vector of numerical classes. I want the number of cells in raster z for each class i, and then divide this by the total number of cells in raster z to get this as a proportion.</p>
  
  <p>Any advice on how I can save this as a matrix/dataframe in a foreach loop?</p>
  
  <p>Thank you in advance.</p>
  
  <hr>
  
  <p><em>Follow up of this <a href="https://stackoverflow.com/questions/20704235/function-not-found-in-r-doparallel-foreach-error-in-task-1-failed-cou">previous question</a></em></p>