[R] Search for locations of subsequences?
Duncan Murdoch
murdoch.duncan at gmail.com
Wed Aug 29 03:10:58 CEST 2012
On 12-08-28 5:45 PM, Rui Barradas wrote:
> 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
Thanks!
Duncan Murdoch
More information about the R-help
mailing list