title: two questions about creating lags within groups and with respect to time
body: <p>I have a data frame <code>x</code> like this:</p>
  
  <pre><code>ID year    month     vol   sum_vol        
  1   2000     1        1       6                 
  1   2000     2        2       6                  
  1   2000     3        3       6                  
  1   2001     3        4       25                  
  1   2001     4        5       25                  
  1   2001     5        16      25                  
  2   2000     1        7       24                
  2   2000     2        8       24                 
  2   2000     3        9       24                
  2   2001     3        12      35                 
  2   2001     4        11      35                 
  2   2001     5        12      35                 
  3   2000     1        13      42                 
  3   2000     2        14      42                 
  3   2000     3        15      42                 
  3   2001     6        16      44          
  3   2001     7        10      44
  3   2001     8        18      44
  </code></pre>
  
  <p>And the desired output:</p>
  
  <pre><code>ID year    month     vol   sum_vol        lag_year_sum_vol      lag_2_month_vol
  1   2000     1        1       6                  NA                    NA
  1   2000     2        2       6                  NA                    NA
  1   2000     3        3       6                  NA                    1
  1   2001     3        4       25                  6                    NA
  1   2001     4        5       25                  6                    NA
  1   2001     5        16      25                  6                    4
  2   2000     1        7       24                 NA                    NA
  2   2000     2        8       24                 NA                    NA
  2   2000     3        9       24                 NA                    7
  2   2001     3        12      35                 24                    NA
  2   2001     4        11      35                 24                    NA
  2   2001     5        12      35                 24                    12
  3   2000     1        13      42                 NA                    NA
  3   2000     2        14      42                 NA                    NA
  3   2000     3        15      42                 NA                    13
  3   2001     6        16      44                 42                    NA
  3   2001     7        10      44                 42                    NA
  3   2001     8        18      44                 42                    16
  </code></pre>
  
  <p>I have searched a lot and not getting a conclusion:</p>
  
  <p>So as you can see my questions are: </p>
  
  <p>1) How to create a variable lag_year_sum_vol for one year per ID, which has a value of last year's sum_vol?</p>
  
  <p>2) How to have lagged new variables lag_2_month_vol per customer per year, by 2 months?</p>
  
  <p><strong>NOTE:</strong> The ID, year and month in the real data may not be in this sorted order. And actually for any arbitrary number for year, month, and vol -> no pattern in the data.</p>
  
  <p>I prefer a method in either dplyr or data.table. (It seems data.table is more concise.)</p>
  
  <p>Thanks in advance!!</p>