title: Assignment inside parallelized foreach loop
body: <p>I've been attempting to run a large parallel operation, but learned to my chagrin that I can't make assignments that stick inside a parallelized <code>foreach</code> loop. That is, attempting to run to following code results in no change to <code>p</code></p>
  
  <pre><code>p &lt;- numeric(3)
  foreach(i=1:3) %dopar% {
    p[i] &lt;- 1
  }
  p
  # [1] 0 0 0
  </code></pre>
  
  <p>I thought it might be an environment issue (i.e., the assignment to <code>p</code> is local), but changing <code>&lt;-</code> to <code>&lt;&lt;-</code> only gave me an error:  <code>Error in { : task 1 failed - "object 'p' not found"</code></p>
  
  <p>Is there some way to either make the subassignment work or work around this problem? </p>
  
  <p>In my real case, <code>p[i] &lt;- 1</code> is actually a subassignment of many elements at once, at random (but predetermined prior to the loop) places in the vector, so taking advantage of something like <code>.combine = c</code> is sadly out of the question.</p>
  
  <h3>What I've tried so far:</h3>
  
  <p>I tried working around this by using <code>.combine = `+`</code>, like so:</p>
  
  <pre><code>s &lt;- foreach(i=1:3, .combine = `+`) %dopar% {
    p &lt;- numeric(3)
    p[i] &lt;- 1
    p
  }
  </code></pre>
  
  <p>While this worked for my small test cases, when I went to apply it to my full size case I got an error (after it ran for about 6 hours, mind you) that R couldn't allocate a vector of size 6.1 GB. Note that this is much larger than the size of the individual several hundred MB vectors each loop is meant to be producing, which I suppose means there was some hidden concatenation that took place.</p>
  
  <h3>Particulars of my case</h3>
  
  <p>My problem involves performing a k-fold cross validation, which means each row of data is assigned a fold <code>1</code> to <code>K</code>, and the foreach loop is looping through the folds <code>k = 1:K</code>, fitting a model on data with <code>folds != k</code>, and then using that model to predict on the remaining data (<code>folds == k</code>). So, ignoring for a moment that this code won't work, I'd like to do something like</p>
  
  <pre><code>folds &lt;- sample(1:K, nrow(mydata), replace = TRUE)
  preds &lt;- numeric(nrow(mydata))
  foreach(k=1:K) %do% {
    m &lt;- fit_model(...)                    # Pseudocode
    preds[folds == k] &lt;- predict_on_model(...) # Pseudocode
  }
  </code></pre>
  
  <p>Thus, my challenge is to get the output of the foreach loop in the correct order.</p>