[R] Pattern Matching

(Ted Harding) Ted.Harding at wlandres.net
Sun Mar 2 21:57:18 CET 2014


On 02-Mar-2014 20:12:57 Benno Pütz wrote:
> Try
> 
> as.numeric(sub(".*\\(","", sub('\\)','',aa)))
> 
> You may also want to look at regexec/regmatches for a more general approach
> ...
> 
> On 02 Mar 2014, at 20:55, Doran, Harold <HDoran at air.org> wrote:
> 
>> "1 (472)" "2 (445)" "3 (431)" "3 (431)" "5 (415)" "6 (405)" "7 (1)”
> 
> Benno Pütz

Another formulation, which breaks it into steps and may therefore
be easier to adopt to similar but different cases, is

  aa0<-gsub("^[0-9]+ ","",aa)
  aa0
  # [1] "(472)" "(445)" "(431)" "(431)" "(415)" "(405)" "(1)"

  as.numeric(gsub("[()]","",aa0))
  # [1] 472 445 431 431 415 405   1

Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 02-Mar-2014  Time: 20:57:07
This message was sent by XFMail




More information about the R-help mailing list