title: elegant way to loop over chunks with remainder in r?
body: <p>I'm looking for some way to iterate over chunks in R, but right now I'm having to add an additional statement at the end to capture the remainder if the number of items does not divide evenly into chunksize. For example:</p>
  
  <pre><code>for (i in 1:(nrow(dataframe)/chunksize)){
    (do something with chunk)
  }
  
  remainder &lt;- nrow(dataframe) %% chunksize
  (do something with dataframe[(length(dataframe)-remainder):length(dataframe),])
  </code></pre>
  
  <p>Is there a more elegant way to do this? I'm assuming this type of operation is done very often in other code.</p>