Search code examples
rmatchvectorization

Find all positions of all matches of one vector of values in second vector


I need to find all positions in my vector corresponding to any of values of another vector:

needles <- c(4, 3, 9)
hay <- c(2, 3, 4, 5, 3, 7)
mymatches(needles, hay) # should give vector: 2 3 5 

Is there any predefined function allowing to do this?


Solution

  • This should work:

    which(hay %in% needles) # 2 3 5