[R] R string help

Phil Spector spector at stat.berkeley.edu
Tue Feb 1 18:50:00 CET 2011


Yan -
    Here's one way.  It assumes there's exactly one set of
brackets in the string, and they can be anywhere:

> aaa<- "up.6.11(16)"
> sub('^.*?\\((.*)\\).*$','\\1',aaa)
[1] "16"


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Tue, 1 Feb 2011, Yan Jiao wrote:

> Dear R guru:
>
>
>
> If I got a variable
>
> aaa<- "up.6.11(16)"
>
>
>
> how can I extract 16 out of the bracket?
>
> I could use substr, e.g.
>
> substr(aaa, start=1, stop=2)
>
> [1] "up"
>
>
>
> But it needs start and stop, what if my start or stop is not fixed, I
> just want the number inside the bracket, how can I achieve this?
>
>
>
> Many thanks
>
>
>
> yan
>
>
> **********************************************************************
> This email and any files transmitted with it are confide...{{dropped:10}}
>
> ______________________________________________
> R-help at r-project.org 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