I have two data frames:
df1 <- data.frame(x1=c("a","b","z","u"),
x2=c("f", "a","d","x"))
df2 <- data.frame(x=letters[1:10],y=1:10,z=11:20)
I now want to merge them by x1,x2 and x, i.e. if the letter x is in either x1 or x2 the corresponding y and z values should be added. If two choices are available x1 should be used as reference.
df1 should be the "master" data set (like all.x = TRUE argument).
the final data frame here would be
x1 x2 y z
a f 1 11
b a 2 12
z d 4 14
u x NA NA
I'd like to see a second solution that adds columns y1, z1, and y2, z2 like this:
x1 x2 y1 z1 y2 z2
a f 1 11 6 16
b a 2 12 1 11
z d NA NA 4 14
u x NA NA NA NA
would appreciate a merge
or dplyr
or tidyr
solution