[datatable-help] Binary search with integer key

Leon Baum leonbaum2 at gmail.com
Sat Jul 30 02:59:53 CEST 2011


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.

Am I missing something obvious?

Leon


More information about the datatable-help mailing list