[R] Pattern Matching

arun smartpink111 at yahoo.com
Sun Mar 2 22:57:14 CET 2014


Hi,
You could try:
as.numeric(gsub(".*[(]([0-9]+)[)]","\\1",aa))
#[1] 472 445 431 431 415 405   1
#or


library(gsubfn)
 strapply(aa,"[(]([0-9]+)[)]",as.numeric,simplify=TRUE)
#[1] 472 445 431 431 415 405   1

A.K.




On Sunday, March 2, 2014 2:57 PM, "Doran, Harold" <HDoran at air.org> wrote:
Suppose I have a character vector as follows:

> aa
[1] "1 (472)" "2 (445)" "3 (431)" "3 (431)" "5 (415)" "6 (405)" "7 (1)”

I want to extract the values within parentheses and place them into a column that is numeric. The values in aa vary in length and so using strsplit() won’t work because the positions change. Is there are better pattern matching function that I can use to grab values from within the parentheses?


    [[alternative HTML version deleted]]

______________________________________________
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