s-news
[Top] [All Lists]

Re: finding substrings

To: Dave Evens <devens8765@yahoo.com>
Subject: Re: finding substrings
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Tue, 13 Sep 2005 04:40:30 -0500
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <20050913092327.19791.qmail@web61324.mail.yahoo.com>
Organization: PDF Solutions, Inc.
References: <20050913092327.19791.qmail@web61324.mail.yahoo.com>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)


Dave Evens wrote:
Dear all,

I would like to find the exact match of a
substring(/sub-vector) within a string (/vector). For
example, suppose I have

x <- c(F, F, F, T, T, F, F, T, T, F, F, T, T, T, F, F,
F, F, F, T, T, T, F, F, T, T, F, F, T, T, T)

y <- c(F, F, T, T, F)

What I would like to do is find the exact match of y
in x with possible overlapping, i.e.
return back

2 6 23

I couldn't get it to work with matchList.

Thanks for any help in advance.

Dave




How about:

x <- paste(as.numeric(x), collapse = "")
y <- paste(as.numeric(y), collapse = "")

loc <- NULL
while((indx <- regexpr(y, x)) > 0) {
  loc <- c(loc, indx + if(length(loc)) loc[length(loc)] else 0)
  x <- substring(x, indx + 1)
}

loc

You'll have to modify this if your vectors are not single characters or cannot pass as.numeric.

HTH,

--sundar

<Prev in Thread] Current Thread [Next in Thread>