[R] separate numbers from chars in a string

arun smartpink111 at yahoo.com
Thu Jul 31 08:54:51 CEST 2014


If you have some variations of the order of numbers followed by chars,

library(stringr)

v1 <- c("absdfds0213451ab", "123abcs4145")
pattern=c("[A-Za-z]+", "\\d+")

do.call(`Map`,c(c,lapply(pattern, function(.pat) str_extract_all(v1, .pat))))
#[[1]]
#[1] "absdfds" "ab"      "0213451"

#[[2]]
#[1] "abcs" "123"  "4145"
A.K.



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 




More information about the R-help mailing list