In R using data.table, how does one subset or query when the criteria field is an integer?

StackOverflow https://stackoverflow.com/questions/8917971

  •  30-10-2019
  •  | 
  •  

문제

I am using data.table package quite a bit. There are lots of examples of subsetting or querying or searching (or whatever you want to call it) with a binary search which is apparently much faster than a vector scan. Here is an excerpt from the help file.

DT["a"]                    # binary search (fast)
DT[x=="a"]                 # vector scan (slow)

But what happens if the the columns one wants to search on is not a factor (or character) but rather is an integer.

cpt <- c(23456,23456,10000,44555,44555)
description <- c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy")
cpt.desc <- data.table(cpt,description)

setkey(cpt.desc,cpt)
cpt.desc[10000,]  

That does not work because the integer 10000 is interpreted as the 10000th row which in this data.table does not exist.

If we modify the syntax then we get what we are looking for.

cpt.desc[cpt==10000,]

However that looks a lot as if it is the slow vector scan method. Is there a binary search function for integers in the data.table package? Thanking you in anticipation of your help.

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top