merge - combine two dataframes in R based on common columns -
i have combine 2 data-frames want take common column among data-frames , join them together. rows in 2 data-frames different.
b c row1 1 0 1 row2 1 0 1
another dataframe
d c f row3 1 0 1 1 row4 1 1 0 0
i want final dataset this
c row1 1 1 row2 1 1 row3 0 1 row4 1 0
here dput 2 dataframes
dput(x1) structure(list(d = c(1l, 1l), = 0:1, c = c(1l, 0l), f = c(1l, 0l)), .names = c("d", "a", "c", "f"), row.names = c("row3", "row4" ), class = "data.frame") dput(x2) structure(list(a = c(1l, 1l), b = c(0l, 0l), c = c(1l, 1l)), .names = c("a", "b", "c"), row.names = c("row1", "row2"), class = "data.frame")
you common names , use row bind:
common <- intersect(names(x1), names(x2)) rbind(x1[,common], x2[,common]) c row3 0 1 row4 1 0 row1 1 1 row2 1 1
edit: match expected output
rbind(x2[,common], x1[,common]) c row1 1 1 row2 1 1 row3 0 1 row4 1 0
Comments
Post a Comment