Skip to content
Prev 173678 / 398528 Next

search for string insider a string

That works.  I want the position just for the purpose of my later manual check.  Thanks a lot Gabor.

-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
Sent: Friday, March 13, 2009 2:18 PM
To: Tan, Richard
Cc: r-help at r-project.org
Subject: Re: [R] search for string insider a string

Try this.  We use regexpr to get the positions and strapply puts the values in list s.  The unlist statement converts NULL to NA and simplifies the list, s, to a numeric vector.  For more info on strapply see http://gsubfn.googlecode.com

library(gsubfn)  # strapply

x <- c"test1", "bcdtestblabla2.1bla", "cdtestblablabla3.88blabla")

dtest.info <- cbind(posn = regexpr("dtest", x),
   value = { s <- strapply(x, "dtest[^0-9]*([0-9][0-9.]*)", as.numeric)
                unlist(ifelse(sapply(s, length), s, NA))
})
pos value
[1,]  NA    NA
[2,]   3  2.10
[3,]   2  3.88

Why do you want the position?   Is there a further transformation needed?
What is it?  There may be even easier approaches to the entire problem.
On Fri, Mar 13, 2009 at 12:25 PM, Tan, Richard <RTan at panagora.com> wrote: