Reputation: 6396
I have two vectors:
a <- c(1, 1, 3, 4, 5, 7, 9)
b <- c(2, 3, 4, 6, 8, 2)
I want to find the numbers in the second vector, which are not in the first vector:
dif <- c(2, 6, 8)
I've tried many different approaches (such as merge, different types of joins (dplyr package), setdiff, compare (compare package)), but I still can't find a way to do it.
Upvotes: 52
Views: 72504
Reputation: 4341
if you want a visual comparison, you can use waldo
a <- c(1, 1, 3, 4, 5, 7, 9)
b <- c(2, 3, 4, 6, 8, 2)
waldo::compare(sort(a), sort(b))
Upvotes: 0
Reputation: 121
The exact question has been answered but if someone wants to find all elements not shared between two lists then this is the answer:
union(setdiff(a,b), setdiff(b,a))
Here 'a' and 'b' could be something like
a<-c(1,2,3,4,5)
b<-c(4,5,6,7,8)
Upvotes: 5
Reputation: 51640
An alternative way, instead of setdiff
(which is probably preferrable), is to use %in%
unique(b[! b %in% a])
#[1] 2 6 8
Upvotes: 19