title: How can I parallelize a double for loop in R?
body: <p>I've been trying to parallelize my code because currently I'm using a double for loop to record results. I've been trying to see how to use the SNOW and doParallel packages in R to do this.</p>
  
  <p>If you would like a replicable example, just use </p>
  
  <pre><code>residual_anomalies &lt;- matrix(sample(c('ANOMALY','NO SIGNAL'),300,replace=T),nrow=100)
  </code></pre>
  
  <p>instead of using these three lines </p>
  
  <pre><code>inputfile &lt;- paste0("simulation_",i,"_",metrics[k],"_US.csv")
  data &lt;- residuals(inputfile)
  
  residual_anomalies &lt;- conceptdrift(data,length=10,threshold=.05)
  </code></pre>
  
  <p>in the nested for loop. The whole code is below.</p>
  
  <pre><code>source("GetMetrics.R")
  source("slowdrift_resampling_vectorized.R")
  
  metrics &lt;- unique(metrics)
  num_metrics &lt;- length(metrics)
  
  f1_scores_table_raw = data.frame(matrix(ncol=10,nrow=46))
  f1_scores_table_pred = data.frame(matrix(ncol=10,nrow=46))
  
  rownames(f1_scores_table_raw) &lt;- metrics
  colnames(f1_scores_table_raw) &lt;- paste0("Sim",1:10)
  
  rownames(f1_scores_table_pred) &lt;- metrics
  colnames(f1_scores_table_pred) &lt;- paste0("Sim",1:10)
  
  
  for(k in 1:num_metrics){
  
    for(i in 1:10){
      #inputfile &lt;- paste0("simulation_",i,"_",metrics[k],"_US.csv")
      #data &lt;- residuals(inputfile)
  
      #residual_anomalies &lt;- conceptdrift(data,length=10,threshold=.05)
  
      #the above is how I get the data frame but I'll create another one for reproducibility.
      residual_anomalies &lt;- as.data.frame(matrix(sample(c('ANOMALY','NO SIGNAL'),300,replace=T),nrow=100))
      names(residual_anomalies) &lt;- c("Raw_Anomaly","Prediction_Anomaly","True_Anomaly")
  
      #calculate precision and recall for an F1 score
  
      #first for raw data
  
      counts &lt;- ifelse(rowSums(residual_anomalies[c("Raw_Anomaly","True_Anomaly")]=='ANOMALY')==2,1,0)
      correct_detections &lt;- sum(counts)
  
      total_predicted = sum(residual_anomalies$Raw_Anomaly =='ANOMALY')
      total_actual = sum(residual_anomalies$True_Anomaly =='ANOMALY')
  
      raw_precision = correct_detections / total_predicted
      raw_recall = correct_detections / total_actual
  
      f1_raw = 2*raw_precision*raw_recall / (raw_precision+raw_recall)
  
      #then for prediction (DLM,ESP,MLR) data
  
      counts &lt;- ifelse(rowSums(residual_anomalies[c("Prediction_Anomaly","True_Anomaly")]=='ANOMALY')==2,1,0)
      correct_detections &lt;- sum(counts)
  
      total_predicted = sum(residual_anomalies$Prediction_Anomaly =='ANOMALY')
      total_actual = sum(residual_anomalies$True_Anomaly =='ANOMALY')
  
      pred_precision = correct_detections / total_predicted
      pred_recall = correct_detections / total_actual
  
      f1_pred = 2*pred_precision*pred_recall / (pred_precision+pred_recall)
  
      f1_scores_table_raw[[k,i]] &lt;- f1_raw
      f1_scores_table_pred[[k,i]] &lt;- f1_pred
    }
  
  }
  </code></pre>
  
  <p>Before, I was using foreach on the outer loop with a %dopar% but the issue I'm having is that I kept getting the issue '%dopar%' not found. Should I parallelize both loops or just one?</p>
  
  <p>Also I know foreach creates a list and stores it into a variable, but can I still have other variables store data in my foreach loop? For example, I still want to record data into my f1_scores_table_raw and f1_scores_table_pred arrays.</p>
  
  <p>Thanks!</p>