I have a question about merge function, there are three dataframe down below,
df1 <- data.frame("id"=c(1:6), "v1"=c(200, 300, 400, 500, 600, 200),
"v2"=c(5, 6, 7, 8, 12, 44))
df2 <- data.frame("id"=c(2, 4, 7, 9), "v3"=c(2, 5, 8, 22))
df3 <- data.frame("id"=c(1, 3, 11, 22), "v3"=c(15, 17, 33, 22))
df1
id v1 v2
1 1 200 5
2 2 300 6
3 3 400 7
4 4 500 8
5 5 600 12
6 6 200 44
df2
id v3
1 2 2
2 4 5
3 7 8
4 9 22
df3
id v3
1 1 15
2 3 17
3 11 33
4 22 22
What I want to do is left join by id by merge function, just like down below,
id v1 v2 v3
1 1 200 5 15
2 2 300 6 2
3 3 400 7 17
4 4 500 8 5
5 5 600 12 NA
6 6 200 44 NA
but my result is
df1 <- merge(df1, df2, by="id", all.x = T)
df1 <- merge(df1, df3, by="id", all.x = T)
df1
id v1 v2 v3.x v3.y
1 1 200 5 NA 15
2 2 300 6 2 NA
3 3 400 7 NA 17
4 4 500 8 5 NA
5 5 600 12 NA NA
6 6 200 44 NA NA
Can someone help me, I can't find the relative question here, thank you!
CodePudding user response:
Try with
merge(df1, rbind(df2, df3), by="id", all.x = T)