title: Lagged difference between rows in R_ a different take
body: <p>My question is similar to a few that have been asked before, but I hope different enough to warrant a separate question. </p>
  
  <p>See <a href="https://stackoverflow.com/questions/39080716/find-difference-between-two-rows-using-dplyr-suppresswarnings-leads-to-na-for">here</a>, and <a href="https://stackoverflow.com/questions/35776233/lagged-differences">here</a>. I'll pull some of the same example data as these questions. For context to my question- I am looking to see how my observed catch-rate (sea creatures) changed over multiple days of sampling the same area. </p>
  
  <p>I want to calculate the difference between the first sample day at a given site (first Letter in data below), and the subsequent sample days (next rows of same letter). </p>
  
  <pre><code> #Example data   
   df &lt;- data.frame(
   id = c("A", "A", "A", "A", "B", "B", "B"), 
   num = c(1, 8, 6, 3, 7, 7 , 9),
   What_I_Want = c(NA, 7, 5, 2, NA, 0, 2))
  </code></pre>
  
  <p>The first solution that I found calculates a lagged difference between each row. I also wanted this calculation- so it was helpful to find:</p>
  
  <pre><code>#Calculate lagged differences
  df_new &lt;- df %&gt;% 
  # group by condition
  group_by(id) %&gt;% 
  # find difference
  mutate(diff = num - lag(num))
  </code></pre>
  
  <p>Here the difference is between A.1 and A.2; then A.2 and A.3 etc...</p>
  
  <p>What I would like to do now is calculate the difference with respect to the first value of each group. So for letter A, I would like to calculate 1 - 8, then 1 - 6, and finally 1 - 3. Any suggestions?</p>
  
  <p>One clunky solution (linked above) is to create two (or more) columns for each distance lagged and some how merge the results that I want</p>
  
  <pre><code>df_clunky = df %&gt;%
  group_by(id) %&gt;%
  mutate(
  deltaLag1 = num - lag(num, 1),
  deltaLag2 = num - lag(num, 2))
  </code></pre>