I'm new to r. This is my first forum question... I'm trying to merge two datasets like these:
df1 <- data.frame(ID = letters[1:5],
x = 5:9,
y = c(NA,6,5,NA,NA))
> df1
ID x y
1 a 5 NA
2 b 6 6
3 c 7 5
4 d 8 NA
5 e 9 NA
df2 <- data.frame(ID = letters[4:8],
y = 7:11,
z = c(4,3,NA,2,1))
> df2
ID y z
1 d 7 4
2 e 8 3
3 f 9 NA
4 g 10 2
5 h 11 1
outcome should look like this:
> df3
ID x y z
1 a 5 NA NA
2 b 6 6 NA
3 c 7 5 NA
4 d 8 7 4
5 e 9 8 3
6 f NA 9 NA
7 g NA 10 2
8 h NA 11 1
I looked around in various forums but couldn't find the solution to this specific problem :/
Any suggestions are much appreciated!
We can use {powerjoin} :
library(powerjoin)
power_full_join(df1, df2, by = "ID", conflict = coalesce_xy)
#> ID x z y
#> 1 a 5 NA NA
#> 2 b 6 NA 6
#> 3 c 7 NA 5
#> 4 d 8 4 7
#> 5 e 9 3 8
#> 6 f NA NA 9
#> 7 g NA 2 10
#> 8 h NA 1 11
Created on 2022-04-14 by the reprex package (v2.0.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With