kgwarishi malatji
kgwarishi malatji

Reputation: 11

Using which and ! functions in R

x <- c("a", "b", "c", "d", "e", "f", "g")
y <- c("a", "c", "d", "z")

I am trying to compare y to x and find an index where in y that does not match with anything in x. in this case z does match and I want R to return the index of z.

This is one of the things I tried and it does not work.

index <- which(y != x)

Upvotes: 0

Views: 41

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389135

You can also use match which will return NA if there is no match.

which(is.na(match(y, x)))
#[1] 4

Or another variation with setdiff

which(y %in% setdiff(y, x))

Upvotes: 1

Filippo
Filippo

Reputation: 351

use the operator %in%

 which(!y%in%x)

Upvotes: 3

Related Questions