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
|