Speed up a function that checks for a sequence
Here is a vectorized version:
yes.seq &lt;- function(dat, pat) {
  paste(dat[dat %in% pat], collapse = &quot;&quot;) == paste(pat, collapse = &quot;&quot;)
}

yes.seq(dat, pat)
# [1] TRUE
yes.seq(dat, rev(pat))
# [1] FALSE