I have a dataset(df1) that looks like this
ID |New York Athletes
Base001 Aaron Judge
Bask001 Kevin Durant
Bask002 Julius Randle
Base002 Max Scherzer
I want to merge in two other datasets at the same time while not adding extra columns
ID |TEAM
Bask001 Nets
Bask002 Knicks
ID |TEAM
Base001 Yankees
Base002 Mets
df1<- df1 %>%
mutate(merge(df1,Base,by="ID",all.x = TRUE))%>%
mutate(merge(.,Base,by="ID",all.x = TRUE))
However when i do this i get
ID |New York Athletes|Teams |Teams.x|Team.y
Base001 Aaron Judge Yankees Yankees
Bask001 Kevin Durant Nets
Bask002 Julius Randle Knicks
Base002 Max Scherzer Mets Mets
I would like to get something like this
ID |New York Athletes|Teams
Base001 Aaron Judge Yankees
Bask001 Kevin Durant Nets
Bask002 Julius Randle Knicks
Base002 Max Scherzer Mets
>Solution :
I think @KU99’s answer is probably the simplest, but here’s another option that uses coalesce.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df1 <- tibble::tribble(
~ID, ~`New York Athletes`,
"Base001", "Aaron Judge",
"Bask001", "Kevin Durant",
"Bask002", "Julius Randle",
"Base002", "Max Scherzer")
df2 <- tibble::tibble(ID = c("Bask001", "Bask002"),
TEAM = c("Nets", "Knicks"))
df3 <- tibble::tibble(ID = c("Base001", "Base002"),
TEAM = c("Yankees", "Mets"))
df1 <- df1 %>%
mutate(merge(.,df2,by="ID",all.x = TRUE))%>%
mutate(merge(.,df3,by="ID",all.x = TRUE)) %>%
select(-TEAM) %>%
mutate(TEAM = coalesce(TEAM.x,TEAM.y )) %>%
select(-c(TEAM.x, TEAM.y))
df1
#> # A tibble: 4 × 3
#> ID `New York Athletes` TEAM
#> <chr> <chr> <chr>
#> 1 Base001 Aaron Judge Yankees
#> 2 Base002 Max Scherzer Mets
#> 3 Bask001 Kevin Durant Nets
#> 4 Bask002 Julius Randle Knicks
Created on 2022-04-15 by the reprex package (v2.0.1)