This is something that I thought I should ask following this question. I'd like to confirm if this is a bug/inconsistency before filing it as a such in the R-forge tracker.
Consider this data.table
:
require(data.table)
DT <- data.table(x=c(1,0,NA), y=1:3)
Now, to access all rows of the DT that are not 0, we could do it in these ways:
DT[x != 0]
# x y
# 1: 1 1
DT[!(x == 0)]
# x y
# 1: 1 1
# 2: NA 3
Accessing DT[x != 0]
and DT[!(x==0)]
gives different results when the underlying logical operation is equivalent.
Note: Converting this into a data.frame and running these operations will give results that are identical with each other for both logically equivalent operations, but that result is different from both these data.table results. For an explanation of why, look at ?`[`
under the section NAs in indexing
.
Edit: Since some of you've stressed for equality with data.frame
, here's the snippet of the output from the same operations on data.frame:
DF <- as.data.frame(DT)
# check ?`[` under the section `NAs in indexing` as to why this happens
DF[DF$x != 0, ]
# x y
# 1 1 1
# NA NA NA
DF[!(DF$x == 0), ]
# x y
# 1 1 1
# NA NA NA
I think this is an inconsistency and both should provide the same result. But, which result? The documentation for [.data.table
says:
i ---> Integer, logical or character vector, expression of column names, list or data.table.
integer and logical vectors work the same way they do in [.data.frame. Other than NAs in logical i are treated as FALSE and a single NA logical is not recycled to match the number of rows, as it is in [.data.frame.
It's clear why the results are different from what one would get from doing the same operation on a data.frame
. But still, within data.table, if this is the case, then both of them should return:
# x y
# 1: 1 1
I went through [.data.table
source code and now understand as to why this is happening. See this post for a detailed explanation of why this is happening.
Briefly, x != 0
evaluates to "logical" and NA
gets replaced to FALSE. However, !(x==0)
, first (x == 0)
gets evaluated to logical and NA
gets replaced to FALSE. Then the negation happens, which results in NA
basically becoming TRUE
.
So, my first (or rather main) question is, is this a bug/inconsistency? If so, I'll file it as one in data.table R-forge tracker. If not, I'd like to know the reason for this difference and I would like to suggest a correction to the documentation explaining this difference (to the already amazing documentation!).
Edit: Following up with comments, the second question is, should data.table
's handling for subsetting by indexing with columns containing NA
resemble that of data.frame
?? (But I agree, following @Roland's comment that this may be very well lead to opinions and I'm perfectly fine with not answering this question at all).
See Question&Answers more detail:
os