The not in
is an operator in R for checking if a value is not contained in a vector. It is the opposite of the in operator, which checks if a value is contained in a vector. It is represented by %!in% syntax and is the Negation of the %in% operator.
The %in% operator is used to identify if an element belongs to a vector. The ! indicates logical negation (NOT). The `not in`operator is cognitively simpler than the more verbose! x %in% table.
The not-in operator is a logical vector, negating the %in% operators on the same arguments.
Please note that the NOT IN(%!in%) is not a built-in operator like the %in% operator, but we can define it using the Negate operator.
Syntax
x %!in% table
Parameters
x
The values to be matched.
table
The values do not match.
Example of not in operator in R
Let’s define two vectors called v1 and v2.
v1 <- 4
v2 <- 11
Now, we define the sequence of 1: 10.
s <- 1:10
Now, we will check the vector values against this sequence and include them in the sequence using in operator.
v1 <- 4
v2 <- 11
s <- 1:10
print(v1 %in% s)
print(v2 %in% s)
Output
[1] TRUE
[1] FALSE
You can see that 4 is in the sequence, so the %in% operator returns TRUE. 11 is not in the sequence, so it returns FALSE.
Let’s use the %!in% operator, but the problem with this is that there is no inbuilt %!in% operator in R.
If you use the %!in% operator in R, you will face the following error.
could not find function “%!in%” in r
To fix this issue, we need to define the %!in% operator. Write the following code to define the Negate %in% operator.
`%!in%` <- Negate(`%in%`)
Now, you can use the %!in% operator.
v1 <- 4
v2 <- 11
t <- 1:10
`%!in%` <- Negate(`%in%`)
print(v1 %!in% t)
print(v2 %!in% t)
Output
[1] FALSE
[1] TRUE
You can see that v1 is included in 1: 10 but not in operator negates this. That is why it returns FALSE.
In the second example, 11 is not included, 1:10, which means negates condition returns TRUE, and it returns TRUE.
That is it.
See also

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.