我必须组合两个看起来像这样的数据帧我想在数据帧中采用公共列并将它们连接在一起.两个数据帧中的行将完全不同.
a b c
row1 1 0 1
row2 1 0 1
Run Code Online (Sandbox Code Playgroud)
另一个数据帧
d a c f
row3 1 0 1 1
row4 1 1 0 0
Run Code Online (Sandbox Code Playgroud)
我希望最终的数据集看起来像这样
a c
row1 1 1
row2 1 1
row3 0 1
row4 1 0
Run Code Online (Sandbox Code Playgroud)
这是来自两个数据帧的输入
dput(x1)
structure(list(d = c(1L, 1L), a = 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")
Run Code Online (Sandbox Code Playgroud)
您可以获取常用名称,然后使用行绑定:
common <- intersect(names(x1), names(x2))
rbind(x1[,common], x2[,common])
a c
row3 0 1
row4 1 0
row1 1 1
row2 1 1
Run Code Online (Sandbox Code Playgroud)
编辑:匹配您的预期输出
rbind(x2[,common], x1[,common])
a c
row1 1 1
row2 1 1
row3 0 1
row4 1 0
Run Code Online (Sandbox Code Playgroud)