title: select data.table R rows based on row number and condition
body: <p>In data.table, we can select rows based on row number or condition:</p>
  
  <pre><code>&gt; x &lt;- data.table(letters[1:4], 1:4)
  &gt; x
     V1 V2
  1:  a  1
  2:  b  2
  3:  c  3
  4:  d  4
  &gt; x[2]
     V1 V2
  1:  b  2
  &gt; x[V1 == "d"]
     V1 V2
  1:  d  4
  </code></pre>
  
  <p>However I can't select with both row number and condition:</p>
  
  <pre><code>&gt; x[!2 &amp; V2 &gt; 1]
  Empty data.table (0 rows) of 2 cols: V1,V2
  </code></pre>
  
  <p>This is probably because the <code>!2</code> is not interpreted as row number in this format. I knew I can chain the two conditions:</p>
  
  <pre><code>&gt; x[!2][V2 &gt; 1]
     V1 V2
  1:  c  3
  2:  d  4
  </code></pre>
  
  <p>However I wanted to assign new column values for this subset</p>
  
  <pre><code>x[!2][V2 &gt; 1, V3 := "more"]
  </code></pre>
  
  <p>Now it only created column for the intermediate chained data.table. I can save the intermediate table then merge back to the original table, but that will be cumbersome.</p>
  
  <p>Actually I often feel <code>data.table</code> need a proper row number. <code>.I</code> is a dynamic number depend on group, but I wanted a unique id that can identify every row, this unique id can be very useful in merge/join (often the data don't have a unique id). If <code>.i</code> is the row number, I can use</p>
  
  <pre><code>x[(.i != 2) &amp; (V2 &gt;1), V3 := "more"]
  </code></pre>
  
  <p>I can simulate this by creating a row number column explicitly first.</p>
  
  <p>Another method is to make the modifications on subset data.table apply back the original table. Suppose we have x as original table, x[!2] as a subset, then if modifications on <code>x[!2]</code> actually modify x, my question will be solved too. Of course this kind of subset need to be created in different way, for example <code>x[!2, refOriginal = TRUE]</code>.</p>