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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:08:49+00:00 2026-06-16T17:08:49+00:00

I have two data.tables, DT and L: > DT = data.table(x=rep(c(a,b,c),each=3), y=c(1,3,6), v=1:9,key=x) >

  • 0

I have two data.tables, DT and L:

> DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9,key="x")
> L=data.table(yv=c(1L:8L,12L),lu=c(letters[8:1],letters[12]),key="yv")

> DT
   x y v
1: a 1 1
2: a 3 2
3: a 6 3
4: b 1 4
5: b 3 5
6: b 6 6
7: c 1 7
8: c 3 8
9: c 6 9

> L
   yv lu
1:  1  h
2:  2  g
3:  3  f
4:  4  e
5:  5  d
6:  6  c
7:  7  b
8:  8  a
9: 12  l

I would like to independently look up the corresponding value of lu from L for column y and for column v in DT. The following syntax provides the correct result, but is cumbersome to generate and then understand at a glance later:

> L[setkey(L[setkey(DT,y)],v)][,list(x,y=yv.1,v=yv,lu.1=lu.1,lu.2=lu)]
   x y v lu.1 lu.2
1: a 1 1    h    h
2: a 2 3    g    f
3: a 3 6    f    c
4: b 4 1    e    h
5: b 5 3    d    f
6: b 6 6    c    c
7: c 7 1    b    h
8: c 8 3    a    f
9: c 9 6   NA    c

(Edit: original post had L[setkey(L[setkey(DT,y)],v)][,list(x,y=yv,v=yv.1,lu.1=lu,lu.2=lu.1)] above, which incorrectly mixed up the y and v columns and looked up values.)

In SQL this would be simple/straightforward:

SELECT DT.*, L1.lu AS lu1, L2.lu AS lu2
FROM DT
LEFT JOIN L AS L1 ON DT.y = L1.yv
LEFT JOIN L AS L2 ON DT.v = L2.yv

Is there a more elegant way to use data.table to perform multiple joins? Note that I’m joining one table to another table twice in this example, but I am also interested in joining one table to multiple different tables.

  • 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-16T17:08:51+00:00Added an answer on June 16, 2026 at 5:08 pm

    Great question. One trick is that i doesn’t have to be keyed. Only x must be keyed.

    There might be better ways. How about this:

    > cbind( L[DT[,list(y)]], L[DT[,list(v)]], DT )
       yv lu yv lu x y v
    1:  1  h  1  h a 1 1
    2:  3  f  2  g a 3 2
    3:  6  c  3  f a 6 3
    4:  1  h  4  e b 1 4
    5:  3  f  5  d b 3 5
    6:  6  c  6  c b 6 6
    7:  1  h  7  b c 1 7
    8:  3  f  8  a c 3 8
    9:  6  c  9 NA c 6 9
    

    or, to illustrate, this is the same :

    > cbind( L[J(DT$y)], L[J(DT$v)], DT )
       yv lu yv lu x y v
    1:  1  h  1  h a 1 1
    2:  3  f  2  g a 3 2
    3:  6  c  3  f a 6 3
    4:  1  h  4  e b 1 4
    5:  3  f  5  d b 3 5
    6:  6  c  6  c b 6 6
    7:  1  h  7  b c 1 7
    8:  3  f  8  a c 3 8
    9:  6  c  9 NA c 6 9
    

    merge could also be used, if the following feature request was implemented :

    FR#2033 Add by.x and by.y to merge.data.table

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

Sidebar

Related Questions

I have two data.table s in R: > tables() NAME NROW MB COLS KEY
I have two SQL tables with data that I would like to compare. The
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have two tables set up in phpmyadmin- table userid and table data. The
I have set up 2 two tables - table userid and table data in
I have two tables with the following (simplified) structures: table "Factors" which holds data
I've got two data tables like below. Table 1: -------------------- sbstart sbend totsb --------------------
I have two data tables which have a primary key column in common but
I have two datasets each with one data table pulled from different sources and
I have these two tables: Table: ORDERS ID - NAME - DATA --------------------------------------------- 1

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.