[datatable-help] Subsetting with logical

Michael Smith my.r.help at gmail.com
Sat Apr 26 03:08:28 CEST 2014


Here's another example, maybe more to the point. Shouldn't the second
line also work, since `b` is logical already?

DT <- data.table(a = 1:8, b = c(TRUE, FALSE))
DT[b]               # Doesn't work.
DT[identity(b)]     # Does work.



On 04/18/2014 07:53 PM, Michael Smith wrote:
> Hi All,
> 
> This is about subsetting using logicals. The code below is
> self-explanatory (I hope). Is this a bug or a feature?
> 
> Thanks,
> 
> M
> 
> 
>> DT <- data.table(a = 1:8, b = c(TRUE, FALSE))
>> ## This does *not* work, but it should (in my humble opinion).
>> DT[b]
> Error in eval(expr, envir, enclos) : object 'b' not found
>> ## This does work, but seems a bit awkward, given that b is already
>> ## logical.
>> DT[b == TRUE]
>    a    b
> 1: 1 TRUE
> 2: 3 TRUE
> 3: 5 TRUE
> 4: 7 TRUE
>> ## With data.frame things work as expected.
>> DF <- as.data.frame(DT)
>> DF[DF$b, ]
>   a    b
> 1 1 TRUE
> 3 3 TRUE
> 5 5 TRUE
> 7 7 TRUE
>> sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-redhat-linux-gnu (64-bit)
> 
> locale:
>  [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C
> LC_TIME=en_US.utf8
>  [4] LC_COLLATE=en_US.utf8     LC_MONETARY=en_US.utf8
> LC_MESSAGES=en_US.utf8
>  [7] LC_PAPER=en_US.utf8       LC_NAME=C                 LC_ADDRESS=C
> 
> [10] LC_TELEPHONE=C            LC_MEASUREMENT=en_US.utf8
> LC_IDENTIFICATION=C
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> other attached packages:
> [1] data.table_1.9.2 colorout_1.0-1
> 
> loaded via a namespace (and not attached):
> [1] plyr_1.8.1     Rcpp_0.11.1    reshape2_1.2.2 stringr_0.6.2
> 


More information about the datatable-help mailing list