title: How do I access counter in a nested foreach loop?
body: <p>So, till date I have written only a handful of R codes (2 projects to be precise), which probably might justify the extent of dumbness in this question that would appear to a seasoned programmer. </p>
  
  <p>I am trying to parallelise my K fold cross validation code, which aims to find the best set of variables to use for final model.</p>
  
  <p>The code is somewhat like this</p>
  
  <pre><code>child &lt;- foreach(i=icount(ncol(parentModel)-1),.combine = 'rbind') %:%{
    childModel&lt;-parentModel
    childModel[,i]&lt;-NULL
    filteredTestMTM &lt;-foreach(j = icount(nFolds),.combine = c, .export =    c("DataSplit","getProbabilityThreshold","SharpeRatio")) %dopar% {
      splitData &lt;- DataSplit(childModel, nFolds = nFolds, testFold=j)
      testData&lt;-splitData$testData
      trainingData&lt;-splitData$trainingData
      trainingMTM &lt;- trainingData[,ncol(trainingData)]
      testMTM &lt;- testData[,ncol(testData)]
      Trade &lt;- (trainingMTM &gt; 0.001)*1.0   #mtmThreshold to be used here instead of 0.001
      trainingData &lt;- trainingData[,1:(ncol(trainingData)-1),drop=FALSE]
      trainingData &lt;- cbind(trainingData, Trade)
  
      logmodel &lt;- glm(Trade ~ .,data=trainingData, family = "binomial"(link="logit") )
      trainingData &lt;- trainingData[,1:(ncol(trainingData)-1),drop=FALSE]
      trainingResults &lt;- predict(logmodel, newdata=trainingData, type="response")
      probabilityThreshold &lt;- getProbabilityThreshold(trainingResults, trainingMTM, 0.001)   #new Probability function to be defined to use optimParam
      tR &lt;- predict(logmodel, newdata=testData, type="response")
      tMTM &lt;- testMTM * ((tR&gt;probabilityThreshold)*1.0)
  
      return(tMTM)
    }
  
    totalSharpe &lt;- (mean(filteredTestMTM)/sd(filteredTestMTM))
  
    if (is.nan(totalSharpe)) {
      totalSharpe = 0.0
    }
  
    return(c(totalSharpe,i ))
  }
  </code></pre>
  
  <p>In summary - I take the parentModel, one by one remove the variables, run K fold cross validation, and collect results. However I keep getting the error</p>
  
  <pre><code>Error in `[&lt;-.data.frame`(`*tmp*`, , i, value = NULL) : 
  object 'i' not found
  </code></pre>
  
  <p>Can anybody please help me with this?</p>
  
  <p>Edit : I am on Windows 7.</p>