找到第二个向量中一个值向量的所有匹配的所有位置

Vas*_*y A 6 r vectorization match

我需要在向量中找到与另一个向量的任何值对应的所有位置:

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

是否有任何预定义的功能允许这样做?

jal*_*pic 12

这应该工作:

which(hay %in% needles) # 2 3 5
Run Code Online (Sandbox Code Playgroud)

  • 针堆里的旧干草:) (4认同)