[R] Equivalent to regMatchPos in R.

Gabor Grothendieck ggrothendieck at gmail.com
Tue Sep 27 23:58:43 CEST 2011


On Tue, Sep 27, 2011 at 5:49 PM, Michael Karol <MKarol at syntapharma.com> wrote:
> R Experts:
>
>
>
> I am trying to isolate the numeric value of day from a string that might
> look like "Cycle 1 Day 8" or "Cycle 12 Day 15".
>
>
>
> In essence, what I need is a function that can look at a character
> string and tell me the location within the string where it matches with
> a given string.  In this case, where within "Cycle 12 Day 15" is the
> text "Day" located.
>
>
>
> The following works in S+, but I haven't been able to locate a function
> within R that does what regMatchPos does in S+.
>

This extracts the numbers from each string converting them to numeric:

library(gsubfn)
x <- c("Cycle 1 Day 8", "Cycle 12 Day 15")
nums <- strapply(x, "\\d+", as.numeric, simplify = TRUE)

which gives:

> nums
     [,1] [,2]
[1,]    1   12
[2,]    8   15


-- 
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