Skip to content
Prev 286340 / 398502 Next

Searching for a pattern within a vector

A different approach to this problem is via convolutional filtering.
  f <- function (x, pattern, tolerance = 1e-05) 
  {
      # x is a numeric matrix (or vector or data.frame) of data, pattern is
      # a vector.  This returns the number of times the pattern is found
      # in each column of x.
      # tolerance is just to account for floating point inaccuracy.
      m <- mean(pattern)
      centeredPattern <- pattern - m
      xp <- filter(as.matrix(x) - m, rev(centeredPattern))
      colSums(abs(xp - sum(centeredPattern^2)) < tolerance, na.rm = TRUE)
  }

E.g., with your example data
  z <- data.frame(
       a = c(1, 1, 1, 0, 0, 1, 1, 1, 1, 1),
       b = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
       c = c(1, 0, 0, 1, 1, 1, 1, 1, 1, 1),
       d = c(0, 0, 0, 1, 1, 1, 0, 0, 0, 0),
       e = c(0, 0, 0, 1, 1, 1, 1, 1, 0, 0))
  pattern <- c(0, 0, 1, 1, 1, 1)
we get
  > f(z, pattern)
  [1] 1 0 1 0 1
  > f(z, pattern) > 0 # what you asked for
  [1]  TRUE FALSE  TRUE FALSE  TRUE

(I've been showing kids on our local FIRST robotics team how
signal/image processing can be done and your example
reminded me of that.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com