Learn R Programming

adagio (version 0.8.4)

occurs: Finding Subsequences

Description

Find subsequences of (integer) sequences.

Usage

occurs(subseq, series)

Value

Returns a vector of indices.

Arguments

subseq

vector of integers.

series

vector of integers.

Details

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.

Examples

Run this code
##  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

if (FALSE) {
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]
}

Run the code above in your browser using DataLab