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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:39:10+00:00 2026-06-01T01:39:10+00:00

The data.table package provides many of the same table handling methods as SQL. If

  • 0

The data.table package provides many of the same table handling methods as SQL. If a table has a key, that key consists of one or more columns. But a table can’t have more than one key, because it can’t be sorted in two different ways at the same time.

In this example, X and Y are data.tables with a single key column “id”; Y also has a non-key column “x_id”.

   X <- data.table(id = 1:5, a=4:8,key="id")
   Y <- data.table(id = c(1,1, 3,5,7), x_id=c(1,4:1), key="id")

The following syntax would join the tables on their keys:

  X[Y]

How can I translate the following SQL syntax to data.table code?

  select * from X join Y on X.id = Y.x_id; 

The closest that I have gotten is:

Y[X,list(id, x_id),by = x_id,nomatch=0]

However, this does not do the same inner join as the SQL statement.


Here is a more clear example in which the foreign key is y_id, and we want the join to look up values of Y2 where X2$y_id = Y2$id.

    X2 <- data.table(id = 1:5, y_id = c(1,1,2,2,2), key="id")
    Y2 <- data.table(id = 1:5, b = letters[1:5], key="id")

I would like to produce the table:

   id  y_id  b
    1     1 "a"
    2     1 "a"
    3     2 "b"
    4     2 "b"
    5     2 "b"

similar to what is done by the following kludge:

> merge(data.frame(X2), data.frame(Y2), by.x = "y_id", by.y = "id")
  y_id id b
1    1  1 a
2    1  2 a
3    2  3 b
4    2  4 b
5    2  5 b

However, when I do this:

    X2[Y2, 1:2,by = y_id]

I do not get the desired result:

    y_id V1
[1,]    1  1
[2,]    1  2
[3,]    2  1
[4,]    2  2
  • 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-01T01:39:11+00:00Added an answer on June 1, 2026 at 1:39 am

    Good question. Note the following (admittedly buried) in ?data.table :

    When i is a data.table, x must have a key. i is joined to x using the key and the rows in x that match are returned. An equi-join is performed between each column in i to each column in x‘s key. The match is a binary search in compiled C in O(log n) time. If i has less columns than x‘s key then many rows of x may match to each row of i. If i has more columns than x‘s key, the columns of i not involved in the join are included in the result. If i also has a key, it is i‘s key columns that are used to match to x‘s key columns and a binary merge of the two tables is carried out.

    So, the key here is that i doesn’t have to be keyed. Only x must be keyed.

    X2 <- data.table(id = 11:15, y_id = c(14,14,11,12,12), key="id")
         id y_id
    [1,] 11   14
    [2,] 12   14
    [3,] 13   11
    [4,] 14   12
    [5,] 15   12
    Y2 <- data.table(id = 11:15, b = letters[1:5], key="id")
         id b
    [1,] 11 a
    [2,] 12 b
    [3,] 13 c
    [4,] 14 d
    [5,] 15 e
    Y2[J(X2$y_id)]  # binary search for each item of (unsorted and unkeyed) i
         id b
    [1,] 14 d
    [2,] 14 d
    [3,] 11 a
    [4,] 12 b
    [5,] 12 b
    

    or,

    Y2[SJ(X2$y_id)]  # binary merge of keyed i, see ?SJ
         id b
    [1,] 11 a
    [2,] 12 b
    [3,] 12 b
    [4,] 14 d
    [5,] 14 d
    
    identical(Y2[J(X2$y_id)], Y2[X2$y_id])
    [1] FALSE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a package that would copy data from a table
R's data.table package offers fast subsetting of values based on keys. So, for example:
I have a data table which already has some values, plus it is getting
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I am using data.table package quite a bit. There are lots of examples of
I would like to use the data.table package in R to dynamically generate aggregations,
I have an SSIS exportation package that is used to export data from an
I can easy export table data by using DBMS_XMLGEN. But is there a package
I recently discovered the data.table package and was now wondering whether or not I
I have an Access table that has a hyperlink field, with the records being

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.