根据同一列

时间:2016-03-07 15:04:48

标签: r filter dataframe

我有一个数据框mydf,我按item_i class_idept_i汇总了mydf。这是汇总后的输出,其中count是item_i class_idept_imydf的观察次数。然后我设置一个计数阈值并删除count < 5的行。我现在想要的是从mydf获取以下数据框中的所有观察结果。例如,所有item_i == -1class_i == 0以及dept_i == 210等等。有任何建议吗?

   item_i class_i dept_i count
1      -1       0    210    30
4      57       0    210     6
10    129       0    210     8
11    130       0    210     9
13    132       0    210     9
28    248       0    210     6

1 个答案:

答案 0 :(得分:1)

使用dplyr包,然后只需

semi_join(mydf, newdf)
#item_i class_i dept_i count
#1     -1       0    210    30
#2     57       0    210     6
#3    129       0    210     8

数据

mydf <- structure(list(item_i = c(-1L, 57L, 129L, 130L, 132L, 248L), 
    class_i = c(0L, 0L, 0L, 0L, 0L, 0L), dept_i = c(210L, 210L, 
    210L, 210L, 210L, 210L), count = c(30L, 6L, 8L, 9L, 9L, 6L
    )), .Names = c("item_i", "class_i", "dept_i", "count"), class = "data.frame", row.names = c("1", 
"4", "10", "11", "13", "28"))
newdf <- structure(list(item_i = c(-1L, 57L, 129L), class_i = c(0L, 0L, 
0L), dept_i = c(210L, 210L, 210L), count = c(30L, 6L, 8L)), .Names = c("item_i", 
"class_i", "dept_i", "count"), row.names = c("1", "4", "10"), class = "data.frame")