Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using 'on' style join with two different column names #2383

Closed
peterlittlejohn opened this issue Sep 25, 2017 · 2 comments
Closed

Using 'on' style join with two different column names #2383

peterlittlejohn opened this issue Sep 25, 2017 · 2 comments
Milestone

Comments

@peterlittlejohn
Copy link

@peterlittlejohn peterlittlejohn commented Sep 25, 2017

Dear data.table team,

I came across a small issue with the use of 'on' in table joins. Or am I misunderstanding the documentation (pasted in below posting):

`

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Platform: x86_64-redhat-linux-gnu (64-bit)
library(data.table)
data.table 1.10.4

d1 <- data.table(A=c(1,2,4),B=c("a","b","d"))
d2 <- data.table(A=c(1,1,4))
d1[d2,on="A"]
A B
1: 1 a
2: 1 a
3: 4 d`

All works well. But now when I try a different column name in d2, it does not work:

`

d1 <- data.table(A=c(1,2,4),B=c("a","b","d"))
d2 <- data.table(W=c(1,1,4))
d1[d2,on=c(x="A",y="W")]
Error in [.data.table(d1, d2, on = c(x = "A", y = "W")) :
Column(s) [x,y] not found in x
`
Am I misunderstanding the documentation (?data.table), which states (in one bullet point on the use of "on"):

  • As a named character vector, e.g., X[Y, on=c(x="a", y="b")]. This is useful when column names to join by are different between the two tables.

Given my understanding over this sentence in the documentation, the code above should produce the identical output as the first example.

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Sep 25, 2017

Sorry, you've misunderstood the documentation. Maybe the wording needs to be cleaned up.

The names of the vector passed to on must be column names in x, and the values must be columns in i:

d1[d2, on = c(A = "W")]

@HughParsonage
Copy link
Member

@HughParsonage HughParsonage commented Sep 25, 2017

You can also do

d1[d2, on = "A==W"]

@mattdowle mattdowle added this to the v1.10.6 milestone Sep 25, 2017
mattdowle added a commit that referenced this issue Sep 25, 2017
Closes #2383; clarifies documentation of on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants