Finding elements that do not overlap between two vectors

song0089 picture song0089 · Feb 5, 2014 · Viewed 41.9k times · Source

I'm trying to identify elements which are not included in the other vector. For instance in two vectors I have

list.a <- c("James", "Mary", "Jack", "Sonia", "Michelle", "Vincent")

list.b <- c("James", "Sonia", "Vincent")

is there a way to verify which people do not overlap? In the example, I would want to get the vector result that contains Mary, Jack, and Michelle.

Any suggestions will help!

Answer

Julius Vainora picture Julius Vainora · Feb 5, 2014

Yes, there is a way:

setdiff(list.a, list.b)
# [1] "Mary"     "Jack"     "Michelle"