As for my comment, what's wrong:
vec <- 1:100 set.seed(2) samp <- sample(length(vec), 30) a <- vec[samp] b <- vec[-samp]
?
To show this separate sets without duplicates:
R> intersect(a, b) integer(0)
If you have duplicate values in your vector, this is another matter, but your question is unclear.
With duplicates in vec everything is a little more complicated, and it depends on what result you wanted to achieve.
R> set.seed(4) R> vec <- sample(100, 100, replace = TRUE) R> set.seed(6) R> samp <- sample(100, 30) R> a <- vec[samp] R> b <- vec[-samp] R> length(a) [1] 30 R> length(b) [1] 70 R> length(setdiff(vec, a)) [1] 41
Thus, setdiff() "does not work" here, since it does not have a length on the right, but then a and b contain duplicate values (but not observations! From the sample):
R> intersect(a, b) [1] 57 35 91 27 71 63 8 92 49 77
Duplicates (intersection) occurs because the above values are repeated twice in the original vec sample
source share