title: Discounted Cumulative Sum in R
body: <p>I'm trying to calculate a discounted cumulative sum in which the later values are worth more.</p>
  
  <p>Let's say I have the following dataset:</p>
  
  <pre><code> dt &lt;- data.table( "year" = c(79,80,81,82,83), "value" = c(5,2,6,8,9))  
  
  &gt; dt
     year value
  1:   79     5
  2:   80     2
  3:   81     6
  4:   82     8
  5:   83     9
  </code></pre>
  
  <p>And I want the following output:</p>
  
  <pre><code>&gt; dt2
  year value     DCS    
  1:   79     5  5.0000  
  2:   80     2  6.5000 
  3:   81     6 11.8500
  4:   82     8 18.6650 
  5:   83     9 25.7985 
  </code></pre>
  
  <p>The discounted cumulative sum (DCS) is calucuted by discounting the previous values at a 10% annual discount rate. So, for the first second line, the DCS value is given by 2 + 5*(0.9)^1. For the third line, the DCS is 6 + (0.9)^1*2 + (0.9)^2*5, and so on.</p>
  
  <p>Formally, the discounted sum formula is given by: </p>
  
  <p><a href="https://i.stack.imgur.com/lgkmU.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/lgkmU.jpg" alt="enter image description here"></a></p>
  
  <p>Finally, a data.table solution is preferable if possible.</p>