+4 votes
in Programming Languages by (5.4k points)
What R function should I use to find the indices of all elements of a small vector (sub-list) in a big vector (list)?

1 Answer

0 votes
by (33.7k points)

You can use the which() function and "%in%" operator together to find the indices of a sublist in a list (vector). 

Here is an example:

> aa <- c(10,12,324,13,345,32,56,73,87,19,23,45,67, 20,42,120, 112)
> bb <- c(32,45,20,67)
> pos = which(aa %in% bb == TRUE)
> pos
[1]  6 12 13 14

...