[datatable-help] Subsetting with logical

Michael Smith my.r.help at gmail.com
Sat Apr 26 04:19:52 CEST 2014


A.K.,

Thanks a lot for the link.

Looking at `?data.table`, maybe the documentation could be changed to
read something like this, with the text in bracket added:

integer and logical vectors work the same way they do in
\code{\link{[.data.frame}} (but see the \dQuote{Advanced} note below
about an exception for single variable names).

The thing is that I did read the documentation, but I stopped reading at
that point because it said to expect the same behavior as with
data.frame, which is not what happened in my example code. And based on
your link to SO, other people have had the same issue too.

M


On 04/26/2014 09:15 AM, arun wrote:
> Hi M,
> 
> Check this link:
> http://stackoverflow.com/questions/16191083/subset-data-table-by-logical-column
> 
> A.K.
> 
>   


On 04/26/2014 09:08 AM, Michael Smith wrote:> 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 Friday, April 18, 2014 7:53 AM, Michael Smith <my.r.help at gmail.com> 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
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
> 


More information about the datatable-help mailing list