I R-users, I am trying to find the way to manipulate a character string to select a 4 digit number after some specific word/s. Example: mytext <- "I do not want the first number 1234, but the second number 5678" Is there any function that allows you to select a certain number of digits (in this case 5678) after a particular word/s (e.g., second number) Thank you for your help Lorenzo [[alternative HTML version deleted]]
Try this: gsub(".*second number ", "", mytext) On Thu, Aug 25, 2011 at 8:00 PM, Lorenzo Cattarino <l.cattarino at uq.edu.au> wrote:> I R-users, > > I am trying to find the way to manipulate a character string to select a 4 digit number after some specific word/s. Example: > > mytext <- "I do not want the first number 1234, but the second number 5678" > > Is there any function that allows you to select a certain number of digits (in this case 5678) after a particular word/s (e.g., second number) > > Thank you for your help > > Lorenzo > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
To be on the safe side in case there are other characters at the end of the string, use:> mytext <- "I do not want the first number 1234, but the second number 5678sadfsadffdsa" > # make sure you get 4 digits > sub("^.*second number[^[0-9]]*([0-9]{4}).*", "\\1", mytext)[1] "5678">On Thu, Aug 25, 2011 at 7:00 PM, Lorenzo Cattarino <l.cattarino at uq.edu.au> wrote:> I R-users, > > I am trying to find the way to manipulate a character string to select a 4 digit number after some specific word/s. Example: > > mytext <- "I do not want the first number 1234, but the second number 5678" > > Is there any function that allows you to select a certain number of digits (in this case 5678) after a particular word/s (e.g., second number) > > Thank you for your help > > Lorenzo > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?