Difference between two vectors in R
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 numbers in the second vector that are not in the first vector:
dif <- c(2, 6, 8)
I've tried many different approaches (such as union, different types of connections (dplyr package), setdiff, compare (compare package)), but I still can't find a way to do this.
+3
Jot eN
source
to share
2 answers
you can use setdiff
setdiff(b,a)
#[1] 2 6 8
+9
akrun
source
to share
An alternative way instead of setdiff
(which is probably preferred) is to use%in%
unique(b[! b %in% a])
#[1] 2 6 8
+3
nico
source
to share