title: R: Dplyr Lagging Variables after Grouping by Multiple Columns
body: <p>I want to calculate the <code>score</code> difference after grouping by <code>Year, State, Tier, Group</code>. A stylised representation of my data would look like:</p>
  
  <pre><code>dat2 &lt;- data.frame(
  Year = sample(1990:1996, 10, replace = TRUE),
  State = sample(c("AL", "CA", "NY"), 10, replace = TRUE),
  Tier = sample(1:2),
  Group = sample(c("A", "B"), 10, replace = TRUE),
  Score = rnorm(10))
  </code></pre>
  
  <p>I tried <code>mutate</code> with <code>group_by_</code> and <code>.dots</code> however it obtains values from the next absolute value (i.e. grouping does not seem to work). I am mostly interested in plotting the yearly differences (ala time-series even though some years would be <code>NA</code>) so this can be solved by either lagging or calculating the next year's score.</p>
  
  <p><strong>Edit:</strong> So, if the dataset looks like:</p>
  
  <pre><code>Year    State    Tier    Group    Score
  1990    AL       1       A        75
  1990    AL       2       A        100
  1990    AL       1       B        5
  1990    AL       2       B        10
  1991    AL       1       A        95
  1991    AL       2       A        80
  1991    AL       1       B        5
  1991    AL       2       B        15
  </code></pre>
  
  <p>The desired end result would be:</p>
  
  <pre><code>Year    State    Tier    Group    Score   Diff
  1991    AL       1       A        95      20     
  1991    AL       1       B        5       0  
  1991    AL       2       A        80      -20
  1991    AL       2       B        15      5
  </code></pre>