[R] string manipulation

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 26 10:06:40 CEST 2011


On Thu, Aug 25, 2011 at 9:51 PM, Lorenzo Cattarino
<l.cattarino at uq.edu.au> wrote:
> Apologies for confusion. What I meant was the following:
>
> mytext <- "I want the number 2000, not the number two thousand"
>
> and the problem is to select "2000" as the first four digits after the word "number". The position of 2000 in the string might change.
>
> thanks
> Lorenzo
>

strapply in gsubfn searches mytext for the indicated regular
expression and passes the back referenced portion (i.e. the portion of
mytext matching the parenthesized portion of the regular expression)
to the as.numeric function whose output is returned.

library(gsubfn)
strapply(mytext, "number.*([0-9]{4})", as.numeric, simplify = TRUE) # 2000

See http://gsubfn.googlecode.com for more info.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list