Vincenzo
Vincenzo

Reputation: 365

How to filter not %in%?

I have a dataframe df with an ID column. I use the following code to filter the values of df contained in a vector of ID:

df <- df %>% 
  filter(ID %in% vector)

How can I filter to get all the df values that have ID not contained in vector instead than contained in vector?

Upvotes: 10

Views: 19286

Answers (2)

Konrad
Konrad

Reputation: 18585

Arguably, the most elegant solution is achieved by leveraging functional programming capabilities available in R and using Negate to create a %nin% function that will return results reverse to those that the are provided by %in%.

library("tidyverse")
`%nin%` <- Negate(`%in%`)
mtcars %>%
  filter(cyl %nin% c(6, 4))
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> ... (truncated for brevity)

Created on 2022-04-07 by the reprex package (v2.0.1)

If you are working in tidyverse you may want to consider using negate. Tidyverse functions play nicely with each other and usually there subtle differences in implementation1

library("tidyverse")
`%nin%` <- negate(`%in%`)
mtcars %>%
  filter(cyl %nin% c(6, 4))
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> ... (truncated for brevity)

Created on 2022-04-07 by the reprex package (v2.0.1)


1Functionals, Advanced R.

Upvotes: 9

danlooo
danlooo

Reputation: 10627

Use ! to invert a condition:

df <- df %>% 
  filter(! ID %in% vector)

Upvotes: 20

Related Questions