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}}
Try this: gsub(".*\\((\\d+)\\).*", "\\1", aaa) On Tue, Feb 1, 2011 at 3:42 PM, Yan Jiao <y.jiao@ucl.ac.uk> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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. >
On Tue, Feb 1, 2011 at 12:42 PM, Yan Jiao <y.jiao at ucl.ac.uk> 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? >strapply in gsubfn is intended for that type of problem. 1. The regular expression matches an open parenthesis "\\(" any string ".*" and then a close parenthesis "\\)" sending the portion within unescaped parentheses (...) to the as.numeric function.> library(gsubfn) > aaa<- "up.6.11(16)" > strapply(aaa, "\\((.*)\\)", as.numeric, simplify = TRUE)[1] 16 2. If you knew it was at the end then the simpler regular expression "(\\d+).$" would also work> strapply(aaa, "(\\d+).$", as.numeric, simplify = TRUE)[1] 16 3. or we could extract all the numbers and take the last one:> strapply(aaa, "\\d+", as.numeric, simplify = ~ tail(x, 1))[1] 16 See: http://gsubfn.googlecode.com -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com