How to filter an R data.table by index and condition
<p>Please have a look at the following sample code.</p>

<pre><code>DT &lt;-data.table(1:15,0,rbinom(15,2,0.5))
</code></pre>

<p>I can filter by condition <code>DT[V3 == 1,]</code> or select rows by index <code>DT[1:5,]</code>.</p>

<p>How can I do both? In the following code, the sequence of the indexed rows seems to by ignored:</p>

<pre><code>DT[V3 == 1 &amp; 1:5]
</code></pre>

<p>I could do <code>DT[1:5,][V3 == 1]</code>, but then, for example, I wouldn't be able to modify the filtered rows:</p>

<pre><code>DT[1:5,][V3 == 1, V2 := 1]
</code></pre>

<p>This only works with the following workaround:</p>

<pre><code>DT[V3 == 1 &amp; DT[,.I &lt;= 5], V2 := 1]
</code></pre>

<p>However, this looks too data.frame-ish to me. Is there a more elegant way and why does <code>DT[V3 == 1 &amp; 1:5]</code> not work?</p>