[R] Search for locations of subsequences?

Rui Barradas ruipbarradas at sapo.pt
Tue Aug 28 23:45:46 CEST 2012


Hello,

Try the following.

occur1 <- function(pat, vec){
     m <- length(pat)
     n <- length(vec)
     candidate <- seq.int(length=n-m+1)
     for (i in seq.int(length=m))
         candidate <- candidate[pat[i] == vec[candidate + i - 1]]
     candidate
}

patrn <- c(1,2,3,4)
exmpl <- c(3,3,4,2,3,1,2,3,4,8,8,23,1,2,3,4,4,34,4,3,2,1,1,2,3,4)

occur1(patrn, exmpl)
[1]  6 13 23

This was posted some time ago. And seems to be the fastest of the 7 
solutions.
https://stat.ethz.ch/pipermail/r-help/2012-February/303756.html

Hope this helps,

Rui Barradas

Em 28-08-2012 22:05, Duncan Murdoch escreveu:
> 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
>
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list