使用R中的dplyr基于单个条件过滤组

时间:2018-01-14 11:44:47

标签: r dataframe dplyr

我有两个数据框。 DF1

    col1
 1  apples
 2 oranges
 3  apples
 4  banana

DF2

   setID    col1
1      1  apples
2      1 oranges
3      1  apples
4      1  banana
5      2  apples
6      2  grapes
7      2 oranges
8      2  apples
9      3 oranges
10     3  grapes
11     3  banana
12     3  banana
13     4  apples
21     4  apples
31     4  banana
41     4 oranges

我使用了来自dplyr包的过滤器来缩小df2 df1 $ col1 [1]并将结果放入tempdf

> tempdf <- df2 %>% filter(df1$col1[1] == df2$col1)
> tempdf
  setID   col1
1     1 apples
2     1 apples
3     2 apples
4     2 apples
5     4 apples
6     4 apples

有没有办法可以看到所有与匹配的&#34; apple&#34;?具有相同setID的元素?喜欢这个

         setID    col1
    1      1  apples
    2      1 oranges
    3      1  apples
    4      1  banana
    5      2  apples
    6      2  grapes
    7      2 oranges
    8      2  apples
    13     4  apples
    21     4  apples
    31     4  banana
    41     4 oranges

1 个答案:

答案 0 :(得分:2)

您可以在any语句中使用filter来过滤df2$col1的任何条目与df1$col1的第一个条目匹配的组。

数据:

df1 = read.table(text="col1
1  apples
2 oranges
3  apples
4  banana",header=T,stringsAsFactors=F)

df2 = read.table(text="   setID    col1
1      1  apples
2      1 oranges
3      1  apples
4      1  banana
5      2  apples
6      2  grapes
7      2 oranges
8      2  apples
9      3 oranges
10     3  grapes
11     3  banana
12     3  banana
13     4  apples
21     4  apples
31     4  banana
41     4 oranges",header=T,stringsAsFactors=F)

代码:

library(dplyr)
df2 %>% group_by(setID) %>% filter(any(col1==df1$col1[1]))

输出:

# A tibble: 12 x 2
# Groups:   setID [3]
   setID    col1
   <int>   <chr>
 1     1  apples
 2     1 oranges
 3     1  apples
 4     1  banana
 5     2  apples
 6     2  grapes
 7     2 oranges
 8     2  apples
 9     4  apples
10     4  apples
11     4  banana
12     4 oranges

希望这有帮助!