Reputation: 9
i'm trying to filter based on a negative statement joined by &
operator. the positive statement gives me the correct stuff i do not want, but somehow when i add in !
to indicate the negative statement (e.g., !(logic1 & logic2)
), it's not returning correctly.
here are the specifics, i want to exclude rows with ID that is in bad AND tagged as "no good" in the column tag
.
bad <- c(101, 103, 107, 110)
the following positive statement returns the correct rows i do not want:
df %>%
filter(ID %in% bad & tag == "no good")
so then i added !
to indicate the negative:
df %>% filter (!(ID %in% bad & tag == "no good"))
somehow, this kicked out the folks with IDs matching bad
regardless of whether tag
had "no good". iow, folks with IDs matching bad
were kicked out even when tag
was blank (NA_character_).
what am i doing wrong here?
thank you!
Upvotes: 0
Views: 5008
Reputation: 887088
We can modify to |
library(dplyr)
df %>%
filter (!(ID %in% bad | tag == "no good"))
# ID tag
#1 105 good
#2 109 good
bad <- c(101, 103, 107, 110)
df <- data.frame(ID=c(101:110),tag = rep(c("good","no good"),5))
Upvotes: 1
Reputation: 462
@phoebe you can get the desired output using a slightly modified approach
df %>% filter (!(ID %in% bad) & tag != "no good")
Generating test dataframe
df
bad <- c(101, 103, 107, 110)
df <- data.frame(ID=c(101:110),tag = rep(c("good","no good"),5))
> df
ID tag
1 101 good
2 102 no good
3 103 good
4 104 no good
5 105 good
6 106 no good
7 107 good
8 108 no good
9 109 good
10 110 no good
Checking output
library(dplyr)
> df %>% filter (!(ID %in% bad) & tag != "no good")
ID tag
1 105 good
2 109 good
Upvotes: 0