Bindini
Bindini

Reputation: 189

How to find common elements on two different length vectors in R?

I need to find common elements in 2 different length vectors.

For example, I have a vector A with 10 elements, and a vector B with 3 elements.

I need get the position of which elements in A is equal to B.

A=c(1,2,45,3,10,5,11,13,6,7)

B=c(45,3,10)

C would be [3,4,5]

I have already tried "match" and "intercept" functions, but no success :(

Thanks a lot! :)

Upvotes: 4

Views: 2584

Answers (1)

nsinghphd
nsinghphd

Reputation: 2022

You can use which function.

> which(A %in% B)
[1] 3 4 5

Upvotes: 4

Related Questions