[R] help with gsub and grep functions

Arne Henningsen ahenningsen at email.uni-kiel.de
Mon Oct 13 18:25:53 CEST 2003


On Monday 13 October 2003 16:30, Martin Olivier wrote:
> Hi all,
>
> Let Names a vector of chatacters. For example,
>
>  > Names
>
> [1] "g 604 be-0 -p1 (602 matches)" "g 606 Phli-0 -p2 (517 matches)"
> [3] "g 608 alu-0  (659 matches)"
>
> I try to use gsub or grep functions for two problems :
>
> 1. First, I would like to delete all the characters between parentheses.
> [1] "g 604 be-0 -p1" "g 606 be-0 -p2"
> [3] "g 608 be-0 -p3"
>
> 2. And, I would like to extract the characters between parentheses
> [1] "602 matches" "517 matches"
> [3] "659 matches"
>
>
> Any idea?
> Best regards,
> Olivier

There might be a better solution, but the following commands do what you want 
(at least in the 3 cases that you showed above):

   sub(" [(].*","",Names)
   sub("[\)]+","",sub("[^(]*[\(]","",Names))

Arne

-- 
Arne Henningsen
Department of Agricultural Economics
Christian-Albrechts-University Kiel
24098 Kiel, Germany
Tel: +49-431-880-4445
Fax: +49-431-880-1397 
ahenningsen at email.uni-kiel.de
http://www.uni-kiel.de/agrarpol/ahenningsen/




More information about the R-help mailing list