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

Join columns mislabelled #137

Closed
ShixiangWang opened this issue Dec 8, 2019 · 4 comments
Closed

Join columns mislabelled #137

ShixiangWang opened this issue Dec 8, 2019 · 4 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@ShixiangWang
Copy link

ShixiangWang commented Dec 8, 2019

Thanks for making this nice tool firstly. I got an error when I tried to rewrite code with dtplyr. I debugged the code and found the left_join() returns the result of right join and vice versa.

library(dplyr, warn.conflicts = FALSE)
library(data.table)
library(dtplyr)


a <- lazy_dt(data.table(x_left = 1:3, y = 1))
b <- lazy_dt(data.table(x_right = 2:4, z = 2))

left_join(a, b, by=c("x_left"="x_right"))

Join result

> left_join(a, b, by=c("x_left"="x_right"))
Source: local data table [?? x 4]
Call:   `_DT2`[`_DT1`, on = .(x_right = x_left), allow.cartesian = TRUE]

  x_right     z     y
    <int> <dbl> <dbl>
1       1    NA     1
2       2     2     1
3       3     2     1

# Use as.data.table()/as.data.frame()/as_tibble() to access results

> left_join(as_tibble(a), as_tibble(b), by=c("x_left"="x_right"))
# A tibble: 3 x 3
  x_left     y     z
   <int> <dbl> <dbl>
1      1     1    NA
2      2     1     2
3      3     1     2
@hadley

This comment has been minimized.

@hadley hadley added the reprex needs a minimal reproducible example label Dec 8, 2019
@ShixiangWang

This comment has been minimized.

@hadley
Copy link
Member

hadley commented Dec 24, 2019

Minimal reprex:

library(data.table)
library(dtplyr)
library(dplyr, warn.conflicts = FALSE)


x1 <- lazy_dt(data.table(x_left = 1:3, y = 1))
x2 <- lazy_dt(data.table(x_right = 2:4, z = 2))

left_join(x1, x2, by = c("x_left" = "x_right"))
#> Source: local data table [?? x 4]
#> Call:   `_DT2`[`_DT1`, on = .(x_right = x_left), allow.cartesian = TRUE]
#> 
#>   x_right     z     y
#>     <int> <dbl> <dbl>
#> 1       1    NA     1
#> 2       2     2     1
#> 3       3     2     1
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

Created on 2019-12-24 by the reprex package (v0.3.0)

I don't think that you're getting a right join (because if that was the case you'd expect x_right to have values 2:4, but instead the column name is being taken from the right hand side instead of the left hand side.

@hadley hadley added bug an unexpected problem or unintended behavior and removed reprex needs a minimal reproducible example labels Dec 24, 2019
@hadley hadley changed the title Left join returns the result of right join Join columns mislabelled Dec 24, 2019
@hadley hadley closed this as completed in 8b84b4e Dec 24, 2019
@ShixiangWang
Copy link
Author

@hadley Thanks for correcting my question and fixing this bug :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants