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

Add 'keep' argument to inner_join? #5581

Closed
patrickbarks opened this issue Oct 31, 2020 · 2 comments · Fixed by #5607
Closed

Add 'keep' argument to inner_join? #5581

patrickbarks opened this issue Oct 31, 2020 · 2 comments · Fixed by #5607
Labels
feature a feature request or enhancement tables 🧮 joins and set operations

Comments

@patrickbarks
Copy link
Contributor

The keep argument, to preserve join key columns from both the x and y data frames (when the name differs), is available for all mutate joins except inner_join. It looks like it should be straightforward to add in the existing call to join_mutate().

Unlike with left/right/full joins, the paired key columns in an inner join will always be identical so admittedly it's less useful to retain both, but still potentially useful in some applications (e.g. to facilitate subsequent joins, or for later data validation).

library(dplyr, warn.conflicts = FALSE)

df1 <- tibble(
  id1 = letters[1:3],
  x = 1:3
)

df2 <- tibble(
  id2 = letters[1:2],
  y = 1:2
)

# use 'keep' arg to retain both join key columns (id1 and id2)
left_join(df1, df2, by = c("id1" = "id2"), keep = TRUE)
#> # A tibble: 3 x 4
#>   id1       x id2       y
#>   <chr> <int> <chr> <int>
#> 1 a         1 a         1
#> 2 b         2 b         2
#> 3 c         3 <NA>     NA

# keep arg also avail for full_join, right_join, and nest_join
# but inner_join doesn't have keep arg so can't retain join key from y
inner_join(df1, df2, by = c("id1" = "id2"), keep = TRUE)
#> # A tibble: 2 x 3
#>   id1       x     y
#>   <chr> <int> <int>
#> 1 a         1     1
#> 2 b         2     2

Created on 2020-10-31 by the reprex package (v0.3.0)

@romainfrancois romainfrancois added the feature a feature request or enhancement label Nov 4, 2020
@hadley hadley added the tables 🧮 joins and set operations label Nov 16, 2020
@hadley
Copy link
Member

hadley commented Nov 16, 2020

@patrickbarks do you want to do a PR?

@patrickbarks
Copy link
Contributor Author

Yes, thanks, just submitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement tables 🧮 joins and set operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants