+5 votes
in Programming Languages by (35.2k 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

+1 vote
by (176k points)
selected by
 
Best answer

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

Related questions

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

...