title: Calculate NPV for cashflows at all point in time
body: <p>I have the following data frame contains cash flows for several items. For example:</p>
  
  <pre><code>test &lt;- data.frame(ID = c(rep("A",3), rep("B",4)), 
          time = c("y3","y2","y1","y4","y3","y2","y1"),
          Cfs= c(rep(1,3),rep(2,4)),
          interest = c(rep(0.1,3),rep(0.05,4)))
  
  ID  time    CFs interest
  A   y3      1   0.1
  A   y2      1   0.1
  A   y1      1   0.1
  B   y4      2   0.05
  B   y3      2   0.05
  B   y2      2   0.05
  B   y1      2   0.05
  </code></pre>
  
  <p>I would like to produce the net present value at each point in time for each item so the final output should look something like this:</p>
  
  <pre><code>ID  time   CFs  interest    NPV
  A   y3     1    0.1         2.487
  A   y2     1    0.1         1.736
  A   y1     1    0.1         0.909
  B   y4     2    0.05        7.092
  B   y3     2    0.05        5.446
  B   y2     2    0.05        3.719
  B   y1     2    0.05        1.905
  </code></pre>
  
  <p>I was able to calculate the NPV of the total cash flows for each item by reading some of the old posts but I am not sure how to do that at each time period. Also, since the actual dataset is quite large (300k+), I am also trying to avoid loops.</p>
  
  <p>Thanks</p>