title: Summary of N recent values
body: <p>I am trying to get summary statistics (sum and max here) with most N recent values.</p>
  
  <p><strong>Starting data:</strong></p>
  
  <pre><code>dt = data.table(id = c('a','a','a','a','b','b','b','b'),
                  week = c(1,2,3,4,1,2,3,4),
                  value = c(2, 3, 1, 0, 5, 7,3,2))
  </code></pre>
  
  <p><strong>Desired result:</strong></p>
  
  <pre><code>dt = data.table(id = c('a','a','a','a','b','b','b','b'),
                      week = c(1,2,3,4,1,2,3,4),
                      value = c(2, 3, 1, 0, 5, 7,3,2),
                      sum_recent2week = c(NA, NA, 5, 4, NA, NA, 12, 10),
                      max_recent2week = c(NA, NA, 3, 3, NA, NA, 7, 7))
  </code></pre>
  
  <p>With the data, I would like to have sum and max of 2 (N=2) most recent values for each row by id. 4th(<code>sum_recent2week</code>) and 5th (<code>max_recent2week</code>) columns are my desired columns</p>