[R] grep(): returning the matched value
Romain Francois
rfrancois at mango-solutions.com
Mon Jan 14 15:13:02 CET 2008
Macnamara, Aidan wrote:
> Hi,
>
> I'm looking to use the grep function (or something else) to return the matched pattern as opposed to the whole element. For example:
>
> x <- c("pjhj24jhjhd")
> grep("[[:digit:]]{2}", x, value=T) returns "people", whereas I simply want "24".
>
> Any help would be appreciated,
>
> Thanks, Aidan
>
Hi,
You need gsub, as in:
R> gsub("^.*([[:digit:]]{2}).*$","\\1", x )
[1] "24"
Cheers,
Romain
--
Mango Solutions
data analysis that delivers
Tel: +44(0) 1249 467 467
Fax: +44(0) 1249 467 468
Mob: +44(0) 7813 526 123
More information about the R-help
mailing list