[R] Searching for a pattern within a vector

William Dunlap wdunlap at tibco.com
Fri Feb 24 18:12:27 CET 2012


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 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Berend Hasselman
> Sent: Friday, February 24, 2012 12:25 AM
> To: Apoorva Gupta
> Cc: r-help at r-project.org
> Subject: Re: [R] Searching for a pattern within a vector
> 
> 
> On 24-02-2012, at 08:30, Apoorva Gupta wrote:
> 
> > Dear R users,
> >
> > I have a data.frame as follows
> >
> >     a b c d e
> > [1,] 1 1 1 0 0
> > [2,] 1 1 0 0 0
> > [3,] 1 1 0 0 0
> > [4,] 0 1 1 1 1
> > [5,] 0 1 1 1 1
> > [6,] 1 1 1 1 1
> > [7,] 1 1 1 0 1
> > [8,] 1 1 1 0 1
> > [9,] 1 1 1 0 0
> > [10,] 1 1 1 0 0
> >
> > Within these 4 vectors, I want to choose those vectors for which I
> > have the pattern (0,0,1,1,1,1) occuring anywhere in the vector.
> > This means I want vectors a,c,e and not b and d.
> >
> 
> See this thread "[R] matching a sequence in a vector?" :
> 
> http://tolstoy.newcastle.edu.au/R/e17/help/12/02/4201.html
> 
> Berend
> 
> ______________________________________________
> 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