djc55
djc55

Reputation: 555

Filtering on two conditions when combined

I would like to filter a data frame only when a combination of two conditions is met. In the example below, I would like to omit any rows in which group = 1 and day = M. How can I do this more efficiently than the solution shown at the bottom:

library(dplyr)

## data
dat <- data.frame(grp = c(1, 1),
                  day = c("M", "F"))

## this doesn't work
dat %>% 
  filter(grp != 1 & day != "M")

## solution but ugly
dat %>% 
  filter(paste0(grp, day) != "1M")

Upvotes: 1

Views: 37

Answers (1)

akrun
akrun

Reputation: 886948

Using setdiff

dat %>% 
   setdiff(dat %>% filter(grp == 1, day == 'M'))

Upvotes: 1

Related Questions