The %in% operator allows comparing two vectors by identifying whether the element of one vector exists in the other.
The rule here is that the vector to be examined will come first, followed by %in% and then the target vector to be compared with.
Working example:
vector1 <- c("Individual 1", "Individual 2", "Individual 3")
vector2 <- c("Individual 1", "Individual 2")
# Examine whether elements of vector1 exist in vector 2
vector1 %in% vector2
## [1] TRUE TRUE FALSE
The outputs are logical values with respect to each element in vector1. Such output can be useful in vector subsetting:
# Get the elements in vector 1 that exist in vector 2
vector1[ vector1 %in% vector2 ]
## [1] "Individual 1" "Individual 2"
To find out the opposite (i.e. which element is absent), wrap the whole expression with an exclamation and a set of parentheses:
!( expression )!(vector1 %in% vector2)
## [1] FALSE FALSE TRUE