hearse
hearse

Reputation: 389

Which- on two vectors of unequal length?

I have two vectors of unequal length, a and b with length(a) being less than b.

I would like to find the indices in 'a' which contain the values in intersect(a,b). How can I achieve this?

Upvotes: 0

Views: 1813

Answers (1)

Alex
Alex

Reputation: 15708

Something like

a <- list(1,2,3,4,5)
b <- list(6,2,1,5,7,9,10)

And you want to identify the position of elements of a in b, use:

which(a %in% b)
# [1] 1 2 5

This also works if a and b are vectors, e.g. a <- c(1,2,3,4,5) and b <- c(6,2,1,5,7,9,10)

Upvotes: 7

Related Questions