I want to know the exact elements of a vector not found in the vector. For instance, consider the following vectors:
veca <- c("ab", "cd", "ef", "gh", "ij", "kl")
vecb <- c("ab", "ef", "ij", "kl")
From this, cd
and gh
are in veca
but not in vecb
. How can one identify these elements in R? Thanks!
We could define a custom function like an opposite intersect function using setdiff
:
learned here:
outersect <- function(x, y) {
sort(c(setdiff(x, y),
setdiff(y, x)))
}
outersect(veca, vecb)
output:
[1] "cd" "gh"
Another possible solution is:
not_in_vecb <- veca[!veca %in% vecb]
[1] "cd" "gh"