title: How can I divide value across periods in R?
body: <p>I'm working with a panel of purchases from a credit card company, which contains several purchases in installments. I only have the total value of those purchases but I'd like to have the actual value the customer pays each month. Here are some sample data:</p>
  
  <pre><code>library(data.table)
  
  aa &lt;- data.table('period' = c(1, 2, 3), 'customer' = 1, 'purchase' = c(90, 20, 10),
                       'installments' = c(3, 2, 1))
  bb &lt;- data.table('period' = c(1, 2, 3), 'customer' = 2, 'purchase' = c(50, 60, 10),
                       'installments' = c(2, 2, 1))
  cc &lt;- rbind(aa, bb)
     period customer purchase installments
  1:      1        1      100            3
  2:      2        1       20            2
  3:      3        1       10            1
  4:      1        2       50            2
  5:      2        2       60            2
  6:      3        2       10            1
  </code></pre>
  
  <p>My desired result is:</p>
  
  <pre><code>   period customer purchase installments spending
  1:      1        1       90            3       30  (90/3)
  2:      2        1       20            2       40  (90/3 + 20/2)            
  3:      3        1       10            1       50  (90/3 + 20/2 + 10)
  4:      1        2       50            2       25  (50/2)
  5:      2        2       60            2       55  (50/2 + 60/2)
  6:      3        2       10            1       40  (60/2 + 10)
  </code></pre>