Quantcast

Indexing by a logical column

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Indexing by a logical column

gsee
Hi,

Is the following expected behavior?

DT = data.table(x=rep(c("a","b","c"),each=3), TF=c(TRUE,FALSE,TRUE))

#All of these return what I expect:

DT[c(TRUE, FALSE, TRUE)]
DT[TF==TRUE]
DT[DT$TF]

#Why doesn't this?
DT[TF]
#Error in eval(expr, envir, enclos) : object 'TF' not found

Thanks,
Garrett
_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Indexing by a logical column

Matthew Dowle

Hi,
Yes expected. From ?data.table:
"Advanced: When i is a single variable name, it is not considered an
expression of column names and is instead evaluated in calling scope."
Subsetting by a logical column is the only example I can think of where
this is confusing. But we make use of this feature quite a lot e.g.
     TMP=list(...);DT[TMP]
safe in the knowledge that DT[TMP] won't start to fail if DT in future
has a column called TMP.
When I have a logical column boolCol I wrap with ():   DT[(boolCol)].  
This avoids the memory allocation and scan of ==TRUE,  and avoids the
variable name repetition of DT[DT$boolCol]
Matthew


On 23.04.2013 19:46, G See wrote:

> Hi,
>
> Is the following expected behavior?
>
> DT = data.table(x=rep(c("a","b","c"),each=3), TF=c(TRUE,FALSE,TRUE))
>
> #All of these return what I expect:
>
> DT[c(TRUE, FALSE, TRUE)]
> DT[TF==TRUE]
> DT[DT$TF]
>
> #Why doesn't this?
> DT[TF]
> #Error in eval(expr, envir, enclos) : object 'TF' not found
>
> Thanks,
> Garrett
> _______________________________________________
> datatable-help mailing list
> [hidden email]
>
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Indexing by a logical column

gsee
Thank you.  Very helpful, as always.

Garrett

On Tue, Apr 23, 2013 at 2:12 PM, Matthew Dowle <[hidden email]> wrote:

>
> Hi,
> Yes expected. From ?data.table:
> "Advanced: When i is a single variable name, it is not considered an
> expression of column names and is instead evaluated in calling scope."
> Subsetting by a logical column is the only example I can think of where this
> is confusing. But we make use of this feature quite a lot e.g.
>     TMP=list(...);DT[TMP]
> safe in the knowledge that DT[TMP] won't start to fail if DT in future has a
> column called TMP.
> When I have a logical column boolCol I wrap with ():   DT[(boolCol)].   This
> avoids the memory allocation and scan of ==TRUE,  and avoids the variable
> name repetition of DT[DT$boolCol]
> Matthew
>
>
>
> On 23.04.2013 19:46, G See wrote:
>>
>> Hi,
>>
>> Is the following expected behavior?
>>
>> DT = data.table(x=rep(c("a","b","c"),each=3), TF=c(TRUE,FALSE,TRUE))
>>
>> #All of these return what I expect:
>>
>> DT[c(TRUE, FALSE, TRUE)]
>> DT[TF==TRUE]
>> DT[DT$TF]
>>
>> #Why doesn't this?
>> DT[TF]
>> #Error in eval(expr, envir, enclos) : object 'TF' not found
>>
>> Thanks,
>> Garrett
>> _______________________________________________
>> datatable-help mailing list
>> [hidden email]
>>
>>
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Loading...