[R] separate numbers from chars in a string

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Jul 30 23:05:11 CEST 2014



On 30.07.2014 22:13, carol white 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


I'd propose something along:

result <- gsub("^([[:alpha:]]+)([[:digit:]]+)([[:alpha:]]+)$", 
"\\1-\\2-\\3", "absdfds0213451ab")

If you have lots of these strings, you can convert all of them and then

do.call("rbind", strsplit(result, "-"))

or some such.

Best,
Uwe Ligges



> Thanks
>
> Carol
> 	[[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