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

  • Home
  • SEARCH
  • 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 8607905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:28:49+00:00 2026-06-12T03:28:49+00:00

How do I select all the rows that have a missing value in the

  • 0

How do I select all the rows that have a missing value in the primary key in a data table.

DT = data.table(x=rep(c("a","b",NA),each=3), y=c(1,3,6), v=1:9)
setkey(DT,x)   

Selecting for a particular value is easy

DT["a",]  

Selecting for the missing values seems to require a vector search. One cannot use binary search. Am I correct?

DT[NA,]# does not work
DT[is.na(x),] #does work
  • 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-06-12T03:28:50+00:00Added an answer on June 12, 2026 at 3:28 am

    Fortunately, DT[is.na(x),] is nearly as fast as (e.g.) DT["a",], so in practice, this may not really matter much:

    library(data.table)
    library(rbenchmark)
    
    DT = data.table(x=rep(c("a","b",NA),each=3e6), y=c(1,3,6), v=1:9)
    setkey(DT,x)  
    
    benchmark(DT["a",],
              DT[is.na(x),],
              replications=20)
    #             test replications elapsed relative user.self sys.self user.child
    # 1      DT["a", ]           20    9.18    1.000      7.31     1.83         NA
    # 2 DT[is.na(x), ]           20   10.55    1.149      8.69     1.85         NA
    

    ===

    Addition from Matthew (won’t fit in comment) :

    The data above has 3 very large groups, though. So the speed advantage of binary search is dominated here by the time to create the large subset (1/3 of the data is copied).

    benchmark(DT["a",],  # repeat select of large subset on my netbook
        DT[is.na(x),],
        replications=3)
              test replications elapsed relative user.self sys.self
         DT["a", ]            3   2.406    1.000     2.357    0.044
    DT[is.na(x), ]            3   3.876    1.611     3.812    0.056
    
    benchmark(DT["a",which=TRUE],   # isolate search time
        DT[is.na(x),which=TRUE],
        replications=3)
                          test replications elapsed relative user.self sys.self
         DT["a", which = TRUE]            3   0.492    1.000     0.492    0.000
    DT[is.na(x), which = TRUE]            3   2.941    5.978     2.932    0.004
    

    As the size of the subset returned decreases (e.g. adding more groups), the difference becomes apparent. Vector scans on a single column aren’t too bad, but on 2 or more columns it quickly degrades.

    Maybe NAs should be joinable to. I seem to remember a gotcha with that, though. Here’s some history linked from FR#1043 Allow or disallow NA in keys?. It mentions there that NA_integer_ is internally a negative integer. That trips up radix/counting sort (iirc) resulting in setkey going slower. But it’s on the list to revisit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a mysql query which will select all rows that contain joe in
If I fill the DataSet with DataAdapters that select all rows from Orders and
I need a MySql statement that will select all the rows, as well as
I would like to delete all the rows found by that query: SELECT cart_abandon.*
I'd like to select all rows from one table which match one or more
I want to write a query to select from a table all rows with
So I have a table that's basically a tree. Each row has a parent
We have a table that has had the same data inserted into it twice
I have created a data frame my.df and wish to select rows (or delete
I am trying to update a table so that rows that are missing are

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.