title: Bin formation in a R data.frame
body: <p>I have a data.frame with two columns:</p>
  
  <pre><code>category quantity
  a          20
  b          30
  c          100
  d          10
  e          1
  f          23
  g          3
  h          200
  </code></pre>
  
  <p>I need to write a function with two parameters: <code>dataframe</code>, <code>bin_size</code> which runs a <code>cumsum</code> over the quantity column, does a split of the subsequent row if the the <code>cumsum</code> exceeds the <code>bin_size</code> and adds a running bin number as an additional column.</p>
  
  <p>Say, by entering this:</p>
  
  <pre><code>function(dataframe, 50)
  </code></pre>
  
  <p>in the above example should give me:</p>
  
  <pre><code>category    quantity    cumsum  bin_nbr
  a            20        20         1
  b            30        50         1
  c            50        50         2
  c            50        50         3
  d            10        10         4
  e            1         11         4
  f           23         34         4
  g            3         37         4
  h            13        50         4
  h            50        50         5
  h            50        50         6
  h            50        50         7
  h            37        37         8
  </code></pre>
  
  <p>Explanation:</p>
  
  <pre><code>row a + b sum up to 50 --&gt; bin_nbr 1
  row c is 100 -&gt; split into 2 rows @ 50 -&gt; bin nbr 2, bin_nbr 3
  row d,e,f,g sum up to 37 -&gt; bin_nbr 4
  I need another 13 from row h to fill in bin_nbr 4 to 50
  The rest of the remaining quantity from h will be spitted into 4 bins -&gt; bin_nbr 5, 6, 7, 8
  </code></pre>