rvectordiff

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 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.


Solution

  • You can use setdiff

    setdiff(b,a)
    #[1] 2 6 8