Find all indices of vector haystack
that are equal to vector needles
,
taking into account the order of both vectors, and their duplicate values. match_all()
is essentially a much more efficient version of:
Like lapply(needles, \(i) which(haystack == i))
, NA
s are ignored. match_all()
internally calls collapse::
fmatch
and collapse::
gsplit.
Core of the code is based on a suggestion by Sebastian Kranz
(author of the 'collapse' package).
Arguments
- needles, haystack
vectors of the same type.
needles
cannot containNA
/NaN
.
Long vectors are not supported.- unlist
Boolean, indicating if the result should be a single unnamed integer vector (
TRUE
, default), or a named list of integer vectors (FALSE
).