Incomplete filtering in dplyr....why?

时间:2015-05-24 21:05:32

标签: r dplyr

Below I have included a subset of my data and some dplyr code. The AA filter statement works as expected. However, the BB filter statement fails to remove all groups specified in Filter.taxa. Any ideas why this is occurring and potential fixes? I have already ensured that spelling and spacing are consistent throughout my data set.

The desired results for BB is provided.

I know that I can code individual filter statements in BB for each group in Filter.taxa, but this is cumbersome. I will be filtering different groups from numerous data sets and would like to specify the groups to be filter in a single line of code.

Thanks in advance.

DATE    SAMPLE  TAXA    GENUS   1
6/30/2013   Aqu-Pool-1  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Pool-2  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Pool-3  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Pool-4  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Riff-1  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Riff-2  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Riff-3  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Riff-4  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Wood-1  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Wood-2  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Wood-3  51  Rhyacophila_nocoh   5
6/30/2013   Aqu-Wood-4  51  Rhyacophila_nocoh   5
11/6/2013   Aqu-Pool-1  43  Arctopsyche_3   10
11/6/2013   Aqu-Pool-2  43  Arctopsyche_3   10
11/6/2013   Aqu-Pool-3  43  Arctopsyche_3   10
11/6/2013   Aqu-Pool-4  43  Arctopsyche_3   10
11/6/2013   Aqu-Riff-1  43  Arctopsyche_3   10
11/6/2013   Aqu-Riff-2  43  Arctopsyche_3   10
11/6/2013   Aqu-Riff-3  43  Arctopsyche_3   10
11/6/2013   Aqu-Riff-4  43  Arctopsyche_3   10
11/6/2013   Aqu-Wood-1  43  Arctopsyche_3   10
11/6/2013   Aqu-Wood-2  43  Arctopsyche_3   10
11/6/2013   Aqu-Wood-3  43  Arctopsyche_3   10
11/6/2013   Aqu-Wood-4  43  Arctopsyche_3   10
11/6/2013   Aqu-Pool-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-4  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-4  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-4  164 Polycentropodidae   1000

Filter.taxa = c("Rhyacophila_nocoh", "Arctopsyche_3")
AA = filter(GENUS %in% Filter.taxa)
BB = filter(GENUS != Filter.taxa) 

Desired result for BB:

DATE    SAMPLE  TAXA    GENUS   1
11/6/2013   Aqu-Pool-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Pool-4  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Riff-4  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-1  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-2  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-3  164 Polycentropodidae   1000
11/6/2013   Aqu-Wood-4  164 Polycentropodidae   1000

1 个答案:

答案 0 :(得分:2)

Use %in% for multiple elements and then negate (!). The != can be used for single element just like ==

 filter(df, !GENUS %in% Filter.taxa)