[datatable-help] Binary search with integer key

Steve Lianoglou mailinglist.honeypot at gmail.com
Sat Jul 30 04:15:19 CEST 2011


Hi Leon,

On Fri, Jul 29, 2011 at 8:59 PM, Leon Baum <leonbaum2 at gmail.com> wrote:
>
> Hello,
>
> How do I perform a binary search on a data.table with an integer key?
>
> Say I create a data.table with
> dt <- data.table(x=11:20, key="x")
>
> Passing the target integer 12 directly to i,
> dt[12, ]
> results in an error because data.table treats 12 as a row number.
>
> Surrounding 12 in quotes,
> dt["12", ]
> results in an error because data.table then expects a character key.
>
> The data.frame notation,
> dt[x == 12, ]
> works, but is slow because data.table appears to be just doing a vector search.

You do it like so:

R> dt[J(12)]

> Am I missing something obvious?

Not sure about "obvious" ... it's possible that you may have been
inspired to try it after reading the intro + faq vignettes, but ...
maybe not :-)

Hope that helps,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the datatable-help mailing list