订阅两个数据帧

时间:2016-02-19 11:49:30

标签: r subscribe

枝, 我有两个这样的数据框,例如

SongInfoHolder SongHolder = new SongInfoHolder(finaObject.getString("_id"),finaObject.getString("decade"),finaObject.getString("artist"),finaObject.getString("song"),finaObject.getString("weeksAtOne"));

df1

V1  V2
a   b
m   n
h   i
l   m
n   i
e   f

我希望在新的数据框中获得相同的行 像这样

 df2

    V1  V2
    a   b
    c   d
    e   f
    b   a

我试过

res2
V1  V2
a   b
e   f
b   a

但我没有成功。还有更好的主意吗?

1 个答案:

答案 0 :(得分:1)

您需要将基于V1 amd V2的两个数据帧与内部联接合并:

df1 <- data.frame(V1 = c("a", "m", "h", "l", "n", "e"), V2 = c("b", "n", "i", "m", "i", "f"), stringsAsFactors = F)

df2 <- data.frame(V1 = c("a", "c", "e"), V2 = c("b", "d", "f"), stringsAsFactors = F)

merge(df1, df2, by = c("V1", "V2"))

结果将是V1V2上的唯一一对df1df2

根据您是否要在df1df2中保留重复值,您也可以使用选项all.x = Tall.y = T