Search code examples
rdplyrmergeleft-join

merge three datasets using left_join and get unique suffixes


Can I merge three datasets using left_join and get unique suffixes for all three datasets?

My dummy data:

df1 <- tibble(x = 1:5, y = c("names", "matches", "multiple", "rows", "different"))
df2 <- tibble(x = 3:5, y = c("first", "second", "third"))
df3 <- tibble(x = 2:4, y = 1:3)
left_join(df1, df2, by='x', suffix = c(".first", ".second"))  %>%
                left_join(., df3 , by='x',  suffix = c("third", "third")) 

#  # A tibble: 5 x 4
#        x y.first   y.second     y
#    <int> <chr>     <chr>    <int>
#  1     1 names     <NA>        NA
#  2     2 matches   <NA>         1
#  3     3 multiple  first        2
#  4     4 rows      second       3
#  5     5 different third       NA

What I'm looking to obtain (the '.third' in the name of the third last column)

#  # A tibble: 5 x 4
#        x y.first   y.second     y.third
#    <int> <chr>     <chr>    <int>
#  1     1 names     <NA>        NA
#  2     2 matches   <NA>         1
#  3     3 multiple  first        2
#  4     4 rows      second       3
#  5     5 different third       NA

Solution

  • try this:

    left_join(df1, df2, by='x', suffix = c(".first", ""))  %>%
      left_join(., df3 , by='x',  suffix = c(".second", ".third")) 
    
          x y.first   y.second y.third
      <int> <chr>     <chr>      <int>
    1     1 names     NA            NA
    2     2 matches   NA             1
    3     3 multiple  first          2
    4     4 rows      second         3
    5     5 different third         NA