title: R data-frame: Inter-row calculations
body: <p>This is an extension of <a href="https://stackoverflow.com/questions/9552771/subtract-previous-years-from-value-from-each-grouped-row-in-data-frame">Subtract previous year&#39;s from value from each grouped row in data frame</a>. The option using plyr makes complete sense. </p>
  
  <p>Now, I'm trying to add a couple more columns. I've also modified year so it's an actual year with different starting points by id. Here is a link to tab-delimited df: <a href="https://dl.dropbox.com/u/9699533/df.txt" rel="nofollow noreferrer">https://dl.dropbox.com/u/9699533/df.txt</a></p>
  
  <p><img src="https://i.stack.imgur.com/emrJ7.png" alt="enter image description here"></p>
  
  <p>I am unable to get a pointer to the previous row when I'm in the current row. I'd like to pass this to the function part of plyr::transform. How do I write this, please? Thanks!</p>
  
  <p>~<em>~</em>~<em>~</em> Alternate dataframe input ~<em>~</em>~<em>~</em>~</p>
  
  <pre><code>&gt; dput(df)
  structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
  4L, 4L, 5L, 5L, 5L), value = c(6L, 16L, 21L, 5L, 10L, 26L, 2L, 
  12L, 26L, 9L, 16L, 26L, 2L, 15L, 29L), year = c(2007L, 2008L, 
  2009L, 2011L, 2012L, 2013L, 2004L, 2004L, 2006L, 2010L, 2011L, 
  2012L, 2014L, 2015L, 2016L), actual = c(6L, 10L, 5L, 5L, 5L, 
  16L, 2L, 10L, 14L, 9L, 7L, 10L, 2L, 13L, 14L)), .Names = c("id", 
  "value", "year", "actual"), class = "data.frame", row.names = c(NA, 
  -15L))
  </code></pre>