[R] String manipulation

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 26 17:24:49 CEST 2011


On Sun, Jun 26, 2011 at 11:00 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Sun, Jun 26, 2011 at 10:54 AM, Megh Dal <megh700004 at yahoo.com> wrote:
>> Dear all, I have following kind of character vector:
>>
>> Vec <- c("344426", "dwjjsgcj", "123sgdc", "aagha123", "sdh343asgh", "123jhd51")
>>
>>
>> Now I want to split each element of this vector according to numeric and string element. For example in the 1st element of that vector, there is no string element. Therefore I should get a vector of length 2 like c("", "344426") and so on.
>>
>> Can somebody point me how to achieve that in R? Is there any specific function for doing that?
>>
>
> Try this and see the gsubfn home page at http://gsubfn.googlecode.com
> for more info:
>
> library(gsubfn)
> strapply(Vec, "\\d+|\\D+", c)
>

Also, if what you want is a leading string which begins Vec[[i]]
followed by a numeric (and everything else is to be ignored) try this:

strapply(Vec, "^(\\D*)(\\d*)", c)

If the first component must be string and you don't want to limit it
to two try this (ignoring the warnings):

L <- strapply(Vec, "\\d+|\\D+", c)
lapply(L, function(x) if (length(x) == 0) x else if
(is.na(as.numeric(x[1]))) x else c("", x))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list