Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which numbers are present in a vector but not in another one [duplicate]

Tags:

r

any

I guess it is a very easy question.

v1 = 1:10
v2 = c(2,4,7)

(none of the numbers are repeated. No need to use unique())

I want a vector containing all the values in v1 that are not in v2.

solution = c(1,3,5,6,8,9,10)

I can do this using a for loop but I'm sure there are easier solution.

like image 379
Remi.b Avatar asked Dec 05 '25 00:12

Remi.b


2 Answers

setdiff(v1, v2)
# [1]  1  3  5  6  8  9 10
like image 134
flodel Avatar answered Dec 06 '25 16:12

flodel


Use the %in% operator with logical NOT ( ! ) to subset v1 by values not in v2:

v1[ ! v1 %in% v2 ]
#[1]  1  3  5  6  8  9 10

Or you could look for non-matches of v1 in v2 (this is almost the same):

v1[ is.na( match( v1 , v2 ) ) ]
#[1]  1  3  5  6  8  9 10

Or using which to get the indices:

v1[  which( ! v1 %in% v2 ) ]
#[1]  1  3  5  6  8  9 10

All flavours of the same thing. And there are many more ways to do this. Definitely don't use a loop for this because this kind of operation is a perfect example of how you can take advatage of R's vectorisation. Loops are better to be called for their side-effects and/or when the processing to number of iterations ratio is large.

like image 43
Simon O'Hanlon Avatar answered Dec 06 '25 15:12

Simon O'Hanlon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!