[R] string manipulation

Lorenzo Cattarino l.cattarino at uq.edu.au
Fri Aug 26 03:51:16 CEST 2011


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

-----Original Message-----
From: Steven Kennedy [mailto:stevenkennedy2263 at gmail.com] 
Sent: Friday, 26 August 2011 11:31 AM
To: Henrique Dallazuanna
Cc: Lorenzo Cattarino; r-help at r-project.org
Subject: Re: [R] string manipulation

You can split your string, and then only take the first 4 digits after
that (this is only an improvement if your numbers might not be at the
end of mytext):

mytext <- "I do not want the first number 1234, but the second number 5678"
sstr<-strsplit(mytext,split="second number ")[[1]][2]
nynumbers<-substr(sstr,1,4)


On Fri, Aug 26, 2011 at 11:18 AM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> 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
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list