title: R dplyr/tidyr: "mutate" new columns with data from other observations
body: <p>I'm trying to create two new variables using dplyr/tidyr functions by using data from previous but related rows.  In my example, I have a data.frame with the data for ID, YEAR and AMOUNT (see below).</p>
  
  <pre><code>    ID   YEAR    AMOUNT
      A    2000    4
      B    2000    4
      A    2001    2
      B    2001    3
      A    2002    3
      B    2002    1
  </code></pre>
  
  <p>I want to create the last two columns dynamically (see below) using dplyr/tidyr (I found a way to do it regularly but I'd be interested to know how to do it in the latest version in dplyr/tidyr).</p>
  
  <pre><code>    ID   YEAR    AMOUNT    YtY_VARIANCE    Y_VARIANCE
      A    2000    4         0               0
      B    2000    4         0               0
      A    2001    2         -2              -2
      B    2001    3         -1              -1
      A    2002    3         +1              -1
      B    2002    1         -2              -3
  </code></pre>
  
  <p>Where YtY_VARIANCE is obtained by subtracting the amount from the previous year and Y_VARIANCE is obtained by subtracting the amount from the first year (always).</p>
  
  <p>I would expect code like this:</p>
  
  <pre><code>    raw_data %&gt;%
          group_by(ID, YEAR) # Not even sure if that's required?!?
          mutate(YtY_VARIANCE = xxx(), Y_VARIANCE = yyy() )
  </code></pre>
  
  <p>What should be xxx and yyy?!  Is that even possible?</p>
  
  <p>Anyone can tell or point me to an answer?</p>
  
  <p>Thanks in advance!</p>