通过比较或当前两个数据帧的子集来查找ID

时间:2017-11-01 21:29:42

标签: r dataframe subset rows

我有两个数据框如下:

=SUMIFS('Sheet1'!$F:$F,'Sheet1'!$C:$C,'Sheet2'!$C5,'Sheet1'!$E:$E,'Sheet2'!$F5,'Sheet1'!$A:$A,">=" & 'Sheet2'!$A5,'Sheet1'!$A:$A,"<=" &'Sheet2'!$B5)

和第二个:

> dput(df1)
structure(list(Freq = c(1L, 1L, 1L, 1L, 0L, 0L, 2L, 1L), x = c(5L, 
2L, 8L, 6L, 3L, 4L, 8L, 6L), y = c(1L, 4L, 2L, 3L, 5L, 6L, 8L, 
8L), ID = 44:51), .Names = c("Freq", "x", "y", "ID"), class = "data.frame",row.names = c(NA, 
-8L))
> str(df1)
'data.frame':   8 obs. of  4 variables:
 $ Freq: int  1 1 1 1 0 0 2 1
 $ x   : int  5 2 8 6 3 4 8 6
 $ y   : int  1 4 2 3 5 6 8 8
 $ ID  : int  44 45 46 47 48 49 50 51

我想从df1找到df2的ID,所以我想要的输出是:

 > dput(df2)
 structure(list(Freq = c(1L, 1L, 1L, 1L), x = c(5L, 2L, 8L, 6L
 ), y = c(1L, 4L, 2L, 3L)), .Names = c("Freq", "x", "y"), class = "data.frame", row.names = c(NA, 
-4L))
 > str(df2)
'data.frame':   4 obs. of  3 variables:
 $ Freq: int  1 1 1 1
 $ x   : int  5 2 8 6
 $ y   : int  1 4 2 3

1 个答案:

答案 0 :(得分:1)

使用 base::merge()

解决方案
# Using OPs data
merge(df1, df2)$ID
# [1] 45 44 47 46

此处merge采用两个数据框并按共享列名称合并(即,df1的子集df2。)