title: Comparing a variable with itself in the previous year in R
body: <p>I still quite new to programming and have a bit of a problem which I haven't been able to find a solution to, although maybe part of that is because I don't know how to phrase my search query (English isn't my first language...). In any case, I have a dataset that is of the following format (simplified; there's data for more years and weeks, and many more goods):</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>GoodID</th>
  <th>Year</th>
  <th>Week</th>
  <th>Price</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>2015</td>
  <td>3</td>
  <td>X1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2015</td>
  <td>4</td>
  <td>Y1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2016</td>
  <td>3</td>
  <td>Z1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2016</td>
  <td>4</td>
  <td>U1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2017</td>
  <td>3</td>
  <td>V1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2017</td>
  <td>4</td>
  <td>W1</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2015</td>
  <td>3</td>
  <td>X2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2015</td>
  <td>4</td>
  <td>Y2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2016</td>
  <td>3</td>
  <td>Z2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2016</td>
  <td>4</td>
  <td>U2</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>What I want to do is add a column that displays the price that a certain good had exactly one year ago. That is, it should display what the price was in the same week, but one year prior.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>GoodID</th>
  <th>Year</th>
  <th>Week</th>
  <th>Price</th>
  <th>Price last year</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>2015</td>
  <td>3</td>
  <td>X1</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2015</td>
  <td>4</td>
  <td>Y1</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2016</td>
  <td>3</td>
  <td>Z1</td>
  <td>X1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2016</td>
  <td>4</td>
  <td>U1</td>
  <td>Y1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2017</td>
  <td>3</td>
  <td>V1</td>
  <td>Z1</td>
  </tr>
  <tr>
  <td>1</td>
  <td>2017</td>
  <td>4</td>
  <td>W1</td>
  <td>U1</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2015</td>
  <td>3</td>
  <td>X2</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2015</td>
  <td>4</td>
  <td>Y2</td>
  <td>NA</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2016</td>
  <td>3</td>
  <td>Z2</td>
  <td>X2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2016</td>
  <td>4</td>
  <td>U2</td>
  <td>Y2</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I'm using tidyverse. Previously I was fine with just having the <em>average</em> across the years, which was easy enough:</p>
  <pre><code>df &lt;- df %&gt;%
  group_by(GoodID, week)%&gt;%
  mutate(avgprice = mean(Price)) %&gt;%
  ungroup()
  </code></pre>
  <p>but when I want to compare prices with the ones a year prior I am completely lost. It sounds like something that should have an easy solution, something like</p>
  <pre><code>mutate(lastprice = Year-1$Price)
  </code></pre>
  <p>but obviously that doesn't work and I don't know how exactly I should word this. I have tried to filter the data</p>
  <pre><code>df &lt;- df %&gt;%
  filter(year == 2017) %&gt;%
  mutate(lastprice = Price)
  
  df &lt;- df %&gt;%
  filter(year == 2016) %&gt;%
  mutate(lastprice = Price)
  </code></pre>
  <p>but this of course only resulted in the data entries from all other years being purged at the first try.</p>
  <p>Does anyone have a suggestion for how I can go about doing this? Thank you!</p>