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 6728875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:13:19+00:00 2026-05-26T10:13:19+00:00

I have a question on the data.table idiom for non-joins, inspired from Iterator’s question

  • 0

I have a question on the data.table idiom for “non-joins”, inspired from Iterator’s question. Here is an example:

library(data.table)

dt1 <- data.table(A1=letters[1:10], B1=sample(1:5,10, replace=TRUE))
dt2 <- data.table(A2=letters[c(1:5, 11:15)], B2=sample(1:5,10, replace=TRUE))

setkey(dt1, A1)
setkey(dt2, A2)

The data.tables look like this

> dt1               > dt2
      A1 B1               A2 B2
 [1,]  a  1          [1,]  a  2
 [2,]  b  4          [2,]  b  5
 [3,]  c  2          [3,]  c  2
 [4,]  d  5          [4,]  d  1
 [5,]  e  1          [5,]  e  1
 [6,]  f  2          [6,]  k  5
 [7,]  g  3          [7,]  l  2
 [8,]  h  3          [8,]  m  4
 [9,]  i  2          [9,]  n  1
[10,]  j  4         [10,]  o  1

To find which rows in dt2 have the same key in dt1, set the which option to TRUE:

> dt1[dt2, which=TRUE]
[1]  1  2  3  4  5 NA NA NA NA NA

Matthew suggested in this answer, that a “non join” idiom

dt1[-dt1[dt2, which=TRUE]]

to subset dt1 to those rows that have indexes that don’t appear in dt2. On my machine with data.table v1.7.1 I get an error:

Error in `[.default`(x[[s]], irows): only 0's may be mixed with negative subscripts

Instead, with the option nomatch=0, the “non join” works

> dt1[-dt1[dt2, which=TRUE, nomatch=0]]
     A1 B1
[1,]  f  2
[2,]  g  3
[3,]  h  3
[4,]  i  2
[5,]  j  4

Is this intended behavior?

  • 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-26T10:13:19+00:00Added an answer on May 26, 2026 at 10:13 am

    As far as I know, this is a part of base R.

    # This works
    (1:4)[c(-2,-3)]
    
    # But this gives you the same error you described above
    (1:4)[c(-2, -3, NA)]
    # Error in (1:4)[c(-2, -3, NA)] : 
    #   only 0's may be mixed with negative subscripts
    

    The textual error message indicates that it is intended behavior.

    Here’s my best guess as to why that is the intended behavior:

    From the way they treat NA‘s elsewhere (e.g. typically defaulting to na.rm=FALSE), it seems that R’s designers view NA‘s as carrying important information, and are loath to drop that without some explicit instruction to do so. (Fortunately, setting nomatch=0 gives you a clean way to pass that instruction along!)

    In this context, the designers’ preference probably explains why NA‘s are accepted for positive indexing, but not for negative indexing:

    # Positive indexing: works, because the return value retains info about NA's
    (1:4)[c(2,3,NA)]
    
    # Negative indexing: doesn't work, because it can't easily retain such info
    (1:4)[c(-2,-3,NA)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the some data which is being transfered from one
I have a question. How could I get the data from a google search
I have a situation similar to the following question: Insert Data Into SQL Table
I have a question: So I have the following data in a table id
I have a question with respect to putting JSON data into a table view.
Dynamic Data question: I have 2 fields of type Nullable<DateTime> on my model When
I have a question concerning Core Data and how, if at all, Entities get
I have an interesting question. Imagine I have a lot of data changing in
Dear g++ hackers, I have the following question. When some data of an object
This is a multi-part question. I have a table similar to: CREATE TABLE sales_data

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.