title: Calculating Lag for dataframe in R?
body: <p>If I have the following dataframe/tibble in R:</p>
  <pre><code>tibble(
    period =
      c(
        &quot;2010Q1&quot;,
        &quot;2010Q2&quot;,
        &quot;2010Q3&quot;,
        &quot;2010Q4&quot;,
        &quot;2010END&quot;,
        &quot;2011Q1&quot;,
        &quot;2011Q2&quot;,
        &quot;2011Q3&quot;,
        &quot;2011Q4&quot;,
        &quot;2011END&quot;,
        &quot;2012Q1&quot;,
        &quot;2012Q2&quot;,
        &quot;2012Q3&quot;,
        &quot;2012Q4&quot;,
        &quot;20120END&quot;,
        &quot;2010Q1&quot;,
        &quot;2010Q2&quot;,
        &quot;2010Q3&quot;,
        &quot;2010Q4&quot;,
        &quot;2010END&quot;,
        &quot;2011Q1&quot;,
        &quot;2011Q2&quot;,
        &quot;2011Q3&quot;,
        &quot;2011Q4&quot;,
        &quot;2011END&quot;,
        &quot;2012Q1&quot;,
        &quot;2012Q2&quot;,
        &quot;2012Q3&quot;,
        &quot;2012Q4&quot;,
        &quot;20120END&quot;),
    website = c(
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;
    ), 
    values = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
  )
  </code></pre>
  <p>How can i perform the lag calculation for each period value from the year prior e.g I would like to create the calculation for 2011Q1 - 2010Q1 and so on including 2011END - 2010END</p>
  <p>So that i get a table which looks like the following:</p>
  <pre><code>tibble(
    period =
      c(
        &quot;2010Q1&quot;,
        &quot;2010Q2&quot;,
        &quot;2010Q3&quot;,
        &quot;2010Q4&quot;,
        &quot;2010END&quot;,
        &quot;2011Q1&quot;,
        &quot;2011Q2&quot;,
        &quot;2011Q3&quot;,
        &quot;2011Q4&quot;,
        &quot;2011END&quot;,
        &quot;2012Q1&quot;,
        &quot;2012Q2&quot;,
        &quot;2012Q3&quot;,
        &quot;2012Q4&quot;,
        &quot;20120END&quot;,
        &quot;2010Q1&quot;,
        &quot;2010Q2&quot;,
        &quot;2010Q3&quot;,
        &quot;2010Q4&quot;,
        &quot;2010END&quot;,
        &quot;2011Q1&quot;,
        &quot;2011Q2&quot;,
        &quot;2011Q3&quot;,
        &quot;2011Q4&quot;,
        &quot;2011END&quot;,
        &quot;2012Q1&quot;,
        &quot;2012Q2&quot;,
        &quot;2012Q3&quot;,
        &quot;2012Q4&quot;,
        &quot;20120END&quot;),
    website = c(
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;google&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;,
      &quot;facebook&quot;
    ), 
    values = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1), 
    calculation = c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,14,12,10,8,6,4,2,0,-2,-4,-6,-8,-10,-12,-14))
  </code></pre>
  <p>Here it makes sense we cannot compare to the period prior so it is NA - for the year 2011 all values are calculated like this:</p>
  <ul>
  <li>2010q1 - NA (nothing before)</li>
  <li>2010q2 - NA (nothing before)</li>
  <li>2010q3 - NA (nothing before)</li>
  <li>2010q4 - NA (nothing before)</li>
  <li>2010END - NA (nothing before)</li>
  <li>2011q1 - 2010q1</li>
  <li>2011q2 - 2010q2</li>
  <li>2011q3 - 2010q3</li>
  <li>2011q4 - 2010q4</li>
  <li>2011END - 2010END</li>
  </ul>
  <p>Having some issues with using the lag() function when trying to group the data by the period column.</p>