R dplyr left join multiple tables without two separate columns with suffix

Suppose I have a main table x

x <- tibble(id = c(1,2,3,4,5), score = c(100,200,300,100,200))
x
# A tibble: 5 x 2
     id score
  <dbl> <dbl>
1     1   100
2     2   200
3     3   300
4     4   100
5     5   200

and two other tables

y = tibble(id = c(1,2), score_new=c(200,300))
y
# A tibble: 2 x 2
     id score_new
  <dbl>     <dbl>
1     1       200
2     2       300

z = tibble(id = c(3,4), score_new = c(300,400))
z
# A tibble: 2 x 2
     id score_new
  <dbl>     <dbl>
1     3       300
2     4       400

If I join them together it will be like this:

x %>% left_join(y, by =c("id" = "id")) %>% left_join(z, by =c("id" = "id"))
# A tibble: 5 x 4
     id score score_new.x score_new.y
  <dbl> <dbl>       <dbl>       <dbl>
1     1   100         200          NA
2     2   200         300          NA
3     3   300          NA         300
4     4   100          NA         400
5     5   200          NA          NA

But I need score_new to be only one column. How do I do that? Sorry if there are already other similar questions but I really couldn't find them.



Read more here: https://stackoverflow.com/questions/68488432/r-dplyr-left-join-multiple-tables-without-two-separate-columns-with-suffix

Content Attribution

This content was originally published by Fryan Fan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: