[R] separate numbers from chars in a string

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Jul 31 10:17:37 CEST 2014



On 31.07.2014 04:46, carol white wrote:
> There are some level of variation either chars followed by numbers or chars, numbers, chars
>
>
> Perhaps, I should use gsub as you suggested all and if the string is composed of chars followed by numbers, it will return the 3rd part empty?


Please read about regularvexpressions and describe your problem 
accurately. If the last strings are onot always present, use * rather 
than + at the very end of the regular expression.

Best,
Uwe Ligges


> Regards,
>
> Carol
>
>
> On Wednesday, July 30, 2014 10:52 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
>
>
>
> On Jul 30, 2014, at 3:13 PM, carol white <wht_crl at yahoo.com> wrote:
>
>> Hi,
>> If I have a string of consecutive chars followed by consecutive numbers and then chars, like "absdfds0213451ab", how to separate the consecutive chars from consecutive numbers?
>>
>> grep doesn't seem to be helpful
>>
>> grep("[a-z]","absdfds0213451ab", ignore.case=T)
>> [1] 1
>>
>>
>>    grep("[0-9]","absdfds0213451ab", ignore.case=T)
>> [1] 1
>>
>> Thanks
>>
>> Carol
>
>
> grep() will only tell you that a pattern is present. You want to use gsub() or similar with back references to return parts of the vector.
>
> Will they ALWAYS appear in that pattern (letters, numbers, letters) or is there some level of variation?
>
> If they will always appear as in your example, then one approach is:
>
>> strsplit(gsub("([a-z]+)([0-9]+)([a-z]+)", "\\1 \\2 \\3", "absdfds0213451ab"), " ")
>
> [[1]]
> [1] "absdfds" "0213451" "ab"
>
>
> The initial gsub() returns the 3 parts separated by a space, which is then used as the split argument to strsplit().
>
> If there will be some variation, you can use multiple calls to gsub() or similar, each getting either the letters or the numbers.
>
> Regards,
>
> Marc Schwartz
> 	[[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.
>



More information about the R-help mailing list