I want to find the indices of all elements of a small vector in a big vector. Is there any R function for it?

+2 votes

Best answer

You can use the **match** function of R to find the positions of all elements of a vector in another vector.

The format of the match function is as follows:

match(x, table, nomatch = NA_integer_, incomparables = NULL)

where, x = your small vector, table = your big vector

Here is an example:

bigVec <- c(11:20)

smallVec <- c(12,14,15,16)match(smallVec, bigVec)

The above code will return 2 4 5 6.