title: data.table within group id
body: <p>I have a data.table with n grouping variables (in this case 2).  I want to add an identifier column for each group as seen in the desired output below.  I tried <code>:=</code>:.N` and I get why that doesn't work but don't know how to make it happen:</p>
  
  <pre><code>library(data.table)
  dat &lt;- data.table::data.table(
      w = 1:16,
      x = LETTERS[1:2],
      y = 1:4
  )[, w := NULL][order(x, y)]
  
  
  ##     x y
  ##  1: A 1
  ##  2: A 1
  ##  3: A 1
  ##  4: A 1
  ##  5: A 3
  ##  6: A 3
  ##  7: A 3
  ##  8: A 3
  ##  9: B 2
  ## 10: B 2
  ## 11: B 2
  ## 12: B 2
  ## 13: B 4
  ## 14: B 4
  ## 15: B 4
  ## 16: B 4
  
  
  dat[, z := 1:.N, by = list(x, y)]
  dat
  </code></pre>
  
  <p><strong>Desired Output</strong></p>
  
  <pre><code>##     x y z
  ##  1: A 1 1
  ##  2: A 1 1
  ##  3: A 1 1
  ##  4: A 1 1
  ##  5: A 3 2
  ##  6: A 3 2
  ##  7: A 3 2
  ##  8: A 3 2
  ##  9: B 2 3
  ## 10: B 2 3
  ## 11: B 2 3
  ## 12: B 2 3
  ## 13: B 4 4
  ## 14: B 4 4
  ## 15: B 4 4
  ## 16: B 4 4
  </code></pre>