Phil's algorithm is a good one, unless you're worried about optimizing for
speed.  It makes N * M comparisons, where N is the length of the first
vector and M is the length of the second.  Explicitly iterating through the
longer vector, you could reduce the number of comparisons to M.  As is often
the case, here there's a tradeoff between human effort and computer effort.

OTOH, because explicit iteration incurs higher interpretation costs in R, it
could conceivably be that Phil's algorithm performs faster.  I don't know.
-- 
View this message in context: 
http://n4.nabble.com/Count-matches-of-a-sequence-in-a-vector-tp2019018p2019428.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to