[R] Splitting strings

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 23 13:55:56 CEST 2007


This applies the indicated perl-style regular expression where the
first backreference (\\D+) is the non-digits and the second
backreference (\\d+) is the digits.

The two backreferences, but not the entire matched pattern itself,
are passed as arguments x and y to the function whose body is the
right hand side of the formula in the third argument.

That is then simplified using rbind to give the result.

library(gsubfn)
strapply(surgery, "(\\D+)(\\d+)", ~ list(lets = x, nums = as.numeric(y)),
   backref = -2, perl = TRUE, simplify = rbind)

More on gsubfn at
  http://gsubfn.googlecode.com


On 8/23/07, Gary Collins <collins.gs at gmail.com> wrote:
> I'm having a Thursday morning mental block, any suggestions on the following
> would be most appreciated...
>
> I have (as an example)
>
> surgery = c("d48",  "d67",  "dnc37",  "a75",  "d10",  "a78",  "d31",
> "d55",  "d1")
>
> before each number part the possibilities are c("a", "d", "dnc"), I'm trying
> to split each element in "surgery" so that I have,
>
> status time
> d        48
> d        67
> dnc     37
> a        75
> d        10
> a        78
> d        31
> d        55
> d        1
>
> I've tried various strsplit approaches but nothing has done what I need.
>
> thanks in advance
>
> Gary
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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