'How to use setdiff on two vectors? [closed]

I have a vector A <- 1:2000 and another vector B with 100 randomly selected values from A. I now want a vector with all the 1900 values in A not in B. I tried to use setdiff(A,B) but this results in a vector C which is 100:2000. I do not quiet get why this is the case since when playing around with setdiff it does give me the right value. I.E setdiff(1:5,3:8)



Solution 1:[1]

setdiff returns the unique values of the first vector not in the second. If we need all the elements, we can use %in% with !

A[!A %in% B]

In the second example

setdiff(1:5, 3:8)

all these values are unique and thus it didn't matter. There is also a package solution for this

library(vecsets)
vsetdiff(A, B)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun