Skip to content

Add 'keep' argument to inner_join? #5581

@patrickbarks

Description

@patrickbarks

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementtables 🧮joins and set operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions