occurs | R Documentation |

Find subsequences of (integer) sequences.

occurs(subseq, series)

`subseq` |
vector of integers. |

`series` |
vector of integers. |

If `m`

and `n`

are the lengths of `s`

and `S`

resp.,
`occurs(s, S)`

determines all positions `i`

such that
`s == S[i, ..., i+m-1]`

.

The code is vectorized and relatively fast. It is intended to complement this with an implementation of Rabin-Karp, and possibly Knuth-Morris-Pratt and Boyer-Moore algorithms.

Returns a vector of indices.

## Examples 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) occurs(patrn, exmpl) ## [1] 6 13 23 ## Not run: set.seed(2437) p = sample(1:20, 1000000, replace=TRUE) system.time(i <- occurs(c(1,2,3,4,5), p)) #=> [1] 799536 ## user system elapsed ## 0.017 0.000 0.017 [sec] ## End(Not run)

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.