On Thu, Aug 25, 2011 at 9:51 PM, Lorenzo Cattarino <l.cattar...@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 ______________________________________________ R-help@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.