Here's an idea. imatch()
will find all the matched indices, in case there is more than one set of matches. It does this by checking two successive indices, one pair at a time, and checking if they are identical to the x
vector. Non-matches are removed, and a list of matches returned.
imatch <- function(x, y) {
Filter(
Negate(is.null),
lapply(seq_along(length(y)-1), function(i) {
ind <- i:(i+1)
if(identical(y[ind], x)) ind
})
)
}
imatch(c(-1, 1), c(1, -1, 1, 0, -1, 0, 0))
# [[1]]
# [1] 2 3
imatch(c(-1, 1), c(1, -1, 1, 0, -1, 1, 0))
# [[1]]
# [1] 2 3
#
# [[2]]
# [1] 5 6