Skip to contents

%nin% finds non-matching elements in a given vector. It is the negation of %in%.

Usage

a %nin% b

Arguments

a

a vector (character, factor, or numeric)

b

a vector (character, factor, or numeric)

Value

%nin% finds non-matching elements and returns one of two things, depending on the use. For two simple vectors, it will report what matches and what does not. For comparing a vector within a data frame, it has the effect of reporting the rows in the data frame that do not match the supplied (second) vector.

Details

This is a simple negation of %in%. I use it mostly for columns in a data frame.

Examples


library(tibble)
library(dplyr)

# Watch this subset stuff

dat <- tibble(x = seq(1:10), d = rnorm(10))
filter(dat, x %nin% c(3, 6, 9))
#> # A tibble: 7 × 2
#>       x      d
#>   <int>  <dbl>
#> 1     1 -0.208
#> 2     2  0.456
#> 3     4  0.571
#> 4     5 -1.31 
#> 5     7  0.394
#> 6     8 -0.125
#> 7    10 -0.628