+4 votes
in Programming Languages by (40.5k points)
I want to find the indices of all elements of a small vector in a big vector. Is there any R function for it?

1 Answer

+2 votes
by (75.9k points)
selected by
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.

Related questions

+5 votes
1 answer
+5 votes
1 answer
+2 votes
1 answer