title: Efficiently Breaking a Loop into "Chunks"
body: <p>I have this code in R:</p>
  <pre><code>final_results &lt;- list()
  
  for (i in 1:339)
  
  {
  
  index_i = i
  num_i = rnorm(1,1,1)
  temp_i = data.frame(index_i,num_i)
  
  final_results[[i]] &lt;- temp_i
     
  }
  
  results_dataframe &lt;- do.call(rbind.data.frame, final_results)
  </code></pre>
  <p>I would like to break this code into &quot;chunks&quot; of 100. For example:</p>
  <pre><code># break into chunks
  # chunk 1
  
   final_results &lt;- list()
      
      for (i in 1:100)
      
      {
      
      index_i = i
      num_i = rnorm(1,1,1)
      temp_i = data.frame(index_i,num_i)
      
      final_results[[i]] &lt;- temp_i
         
      }
      
      results_dataframe_1 &lt;- do.call(rbind.data.frame, final_results)
  
  # chunk 2
  final_results &lt;- list()
      
      for (i in 101:201)
      
      {
      
      index_i = i
      num_i = rnorm(1,1,1)
      temp_i = data.frame(index_i,num_i)
      
      final_results[[i]] &lt;- temp_i
         
      }
      
      results_dataframe_2 &lt;- do.call(rbind.data.frame, final_results)
  
  # chunk 3
  
  final_results &lt;- list()
      
      for (i in 202:302)
      
      {
      
      index_i = i
      num_i = rnorm(1,1,1)
      temp_i = data.frame(index_i,num_i)
      
      final_results[[i]] &lt;- temp_i
         
      }
      
      results_dataframe_3 &lt;- do.call(rbind.data.frame, final_results)
  
  # chunk 4
  
  final_results &lt;- list()
      
      for (i in 303:339)
      
      {
      
      index_i = i
      num_i = rnorm(1,1,1)
      temp_i = data.frame(index_i,num_i)
      
      final_results[[i]] &lt;- temp_i
         
      }
      
      results_dataframe_4 &lt;- do.call(rbind.data.frame, final_results)
  
  # bind everything together
  
  final_result = rbind(results_dataframe_1, results_dataframe_2, results_dataframe_3, results_dataframe_4)
  </code></pre>
  <p><strong>I am looking for a more efficient way to do this.</strong> For example, suppose the index went from 1:887 - I would have to copy and modify this code several times.</p>
  <p>I thought of the following way to start this problem - I can &quot;break the index&quot; into some arbitrary number of &quot;chunks&quot;:</p>
  <pre><code>d = 1:339
  res&lt;-as.integer(quantile(d, probs=seq(0.1,1, by=0.1)))
  
     [1]  34  68 102 136 170 203 237 271 305 339
  </code></pre>
  <ul>
  <li>But is there a way to take these quantiles and efficiently run the loop by these &quot;chunks&quot;?</li>
  </ul>
  <p>Thank you!</p>