从列中相似的两个data.frames中提取行?

时间:2013-07-18 18:53:55

标签: r

对于以下两个data.frame s

Set1 <- data.frame(Object=c("one","two","three","four"),
                   Age=c(1,1,1,1),
                   Value=c(1,2,4,8))
Set2 <- data.frame(Object=c("one","two","three","five"),
                   Age=c(2,2,2,2),
                   Value=c(4,8,2,7))

我想在Set1Set2中获取重复的条目(根据“对象”一栏),即

  Object Age Value
1    one   1     1
2    two   1     2
3  three   1     4
4    one   2     4
5    two   2     8
6  three   2     2

我将如何做到这一点?

2 个答案:

答案 0 :(得分:3)

> x = intersect(Set1$Object, Set2$Object)
> rbind(Set1[Set1$Object %in% x,], Set2[Set2$Object %in% x,])
  Object Age Value
1    one   1     1
2    two   1     2
3  three   1     4
4    one   2     4
5    two   2     8
6  three   2     2

答案 1 :(得分:0)

由于我不确定我是否理解你的问题(你的例子不符合任何问题),我只能建议提示:

设置&lt; - rbind(Set1,Set2)

rv&lt; - 设置[设置[,“对象”]%%重复(设置[,“对象”]),]