1
votes

I have two dataframes which I wish to combine. I would like to replace existing data in df1 with df2's by matching names at the same time combining a new row, retaining data in df1 which does not exist in df2, as well as merging a new row from df2 that does not exist in df1. Example:

df1:

name       age
A          15
B          15
C          16
D          17
E          18

df2:

name       age       height
A          15        159
B          15        156
C          20        160
F          21        171
G          22        163

Desired outcome:

name       age       height
A          15        159
B          15        156
C          20        160 
D          17        NA
E          18        NA
F          21        171
G          22        163

See that aside from new column being combined, row C in df1 has the age column updated from df2, with D & E maintained, & F & G merged into the dataframe. Is there any suggestion on how to do this? Thank you in advance

1

1 Answers

1
votes
library(dplyr) 
df1 %>% full_join(df2, by = "name") %>%
  mutate(age = coalesce(age.y, age.x)) %>%
  select(-age.y, -age.x)