On 12-08-28 5:32 PM, Marc Schwartz wrote:
On Aug 28, 2012, at 4:05 PM, Duncan Murdoch <murdoch.dun...@gmail.com> wrote:

Is there a function to efficiently search for a subsequence within a vector?

For example, with

x <- 1:100

I'd like to search for the sequence c(49,50,51), and be told that it occurs 
exactly once, starting at location 49.  (The items in the vectors might be 
numeric or character, and there might be repetitions within the search pattern 
or within the vector I'm searching.)

Duncan Murdoch


There is a thread here:

   http://tolstoy.newcastle.edu.au/R/e17/help/12/02/4201.html

that has various solutions and towards the end has a post with some timings.

Thanks, that's helpful. I wonder if we should have a function based on the fast solution in that thread (or something even faster written all in compiled code; I suspect Boyer-Moore could do well, but not coded in R).

Duncan Murdoch

______________________________________________
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