Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is documented and consistent behaviour. </p> <p>The main thing to note is that the prefix <code>!</code> within the <code>i</code> argument is a flag for a not join, so <code>x != 0</code> and <code>!(x==0)</code> are no longer the same logical operation <strong>when working with the documented handling of NA within data.table</strong></p> <p>The section from the news regarding the <code>not join</code></p> <pre><code>A new "!" prefix on i signals 'not-join' (a.k.a. 'not-where'), #1384i. DT[-DT["a", which=TRUE, nomatch=0]] # old not-join idiom, still works DT[!"a"] # same result, now preferred. DT[!J(6),...] # !J == not-join DT[!2:3,...] # ! on all types of i DT[colA!=6L | colB!=23L,...] # multiple vector scanning approach (slow) DT[!J(6L,23L)] # same result, faster binary search '!' has been used rather than '-' : * to match the 'not-join'/'not-where' nomenclature * with '-', DT[-0] would return DT rather than DT[0] and not be backwards compatible. With '!', DT[!0] returns DT both before (since !0 is TRUE in base R) and after this new feature. * to leave DT[+J...] and DT[-J...] available for future use </code></pre> <p>And from <code>?data.table</code></p> <blockquote> <p>All types of 'i' may be prefixed with !. This signals a not-join or not-select should be performed. Throughout data.table documentation, where we refer to the type of 'i', we mean the type of 'i' after the '!', if present. See examples.</p> </blockquote> <hr> <p>Why is it consistent with the documented handling of NA within data.table</p> <p><code>NA</code> values are considered FALSE. Think of it like doing <code>isTRUE</code> on each element. </p> <p>so <code>DT[x!=0]</code> is indexed with <code>TRUE FALSE NA</code> which becomes <code>TRUE FALSE FALSE</code> due to the documented NA handling. </p> <p>You are wanting to subset when things are TRUE.</p> <p>This means you are getting those where x != 0 is TRUE ( and not NA)</p> <p><code>DT[!(x==0)]</code> uses the <strong>not join</strong> states you want everything that is not 0 (which can and will include the <code>NA</code> values). </p> <hr> <h2>follow up queries / further examples</h2> <h3><code>DT[!(x!=0)]</code></h3> <pre><code>## returns x y 1: 0 2 2: NA 3 </code></pre> <p><code>x!=0</code> is TRUE for one value, so the not join will return what isn't true. (ie what was <code>FALSE</code> (actually <code>== 0</code>) or <code>NA</code> </p> <h2><code>DT[!!(x==0)]</code></h2> <pre><code>## returns x y 1: 0 2 2: NA 3 </code></pre> <p>This is parsed as <code>!(!(x==0))</code>. The prefix <code>!</code> denotes a not join, and the inner <code>!(x==0)</code> is parsed identically to <code>x!=0</code>, so the reasoning from the case immediately above applies.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload