Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:53:14+00:00 2026-05-28T05:53:14+00:00

I am using data.table quite a lot. It works well but I am finding

  • 0

I am using data.table quite a lot. It works well but I am finding it is taking me a long time to transition my syntax so that it takes advantage of the binary searching.

In the following data table how would 1 select all the rows, including where the CPT value is NA but exclude rows where the CPT value is 23456 or 10000.

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

setkey(cpt.desc,cpt)

The following line works but I think it uses the vector scan method instead of a binary search (or binary exclusion). Is there a way to to drop rows by binary methods?

cpt.desc[!cpt %in% c(23456,10000),]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:53:15+00:00Added an answer on May 28, 2026 at 5:53 am

    Only a partial answer, because I am new to data.table. A self-join works for number, but the same fails for strings. I am sure one of the professional data tablers knows what to do.

    library(data.table)
    
    n <- 1000000
    cpt.desc <- data.table(
      cpt=rep(c(23456,23456,10000,44555,44555,NA),n),
      description=rep(c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy","miscellaneous procedure"),n))
    
    # Added on revision. Not very elegant, though. Faster by factor of 3
    # but probably better scaling 
    setkey(cpt.desc,cpt)
    system.time(a<-cpt.desc[-cpt.desc[J(23456,45555),which=TRUE]])
    system.time(b<-cpt.desc[!(cpt %in% c(23456,45555))] )
    str(a)
    str(b)
    
    identical(as.data.frame(a),as.data.frame(b))
    
    # A self-join works Ok with numbers
    setkey(cpt.desc,cpt)
    system.time(a<-cpt.desc[cpt %in% c(23456,45555),])
    system.time(b<-cpt.desc[J(23456,45555)])
    str(a)
    str(b)
    
    identical(as.data.frame(a),as.data.frame(b)[,-3])
    
    # But the same failes with characters
    setkey(cpt.desc,description)
    system.time(a<-cpt.desc[description %in% c("castration","orchidectomy"),])
    system.time(b<-cpt.desc[J("castration","orchidectomy"),])
    identical(as.data.frame(a),as.data.frame(b)[,-3])
    
    str(a)
    str(b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that I am using to copy data from a table
I am using data.table package quite a bit. There are lots of examples of
I am trying to import an excel file into a data table using GemBox
I am using a Windows application for exporting a data table to Excel. It's
I'm using MyFaces 1.1.7 with Facelets and Tomahawk. When creating a regular data table,
I'm trying to reset some input text fields, inside of a data table, using
I'm using System.Data.OracleClient.OracleCommand to create a table and fill it out with some data.
Using PHP, I can convert MySQL data or static table data to csv, Excel,
I was trying to load a data file into mysql table using LOAD DATA
I am using VSTO to fill data into a table in a Microsoft Word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.