'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?
Solution 1:[1]
Use ! to invert a condition:
df <- df %>%
filter(! ID %in% vector)
Solution 2:[2]
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 already available %in% provides.
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danlooo |
| Solution 2 |
