[R] Quick GREP challenge

David Winsemius dwinsemius at comcast.net
Thu Aug 26 15:35:53 CEST 2010


On Aug 26, 2010, at 8:15 AM, Gabor Grothendieck wrote:

> On Thu, Aug 26, 2010 at 6:16 AM, Dimitri Shvorob
> <dimitri.shvorob at gmail.com> wrote:
>>
>>> grep("f[0-9]+=", "f1=5,f22=3,", value = T)
>> [1] "f1=5,f22=3,"
>>
>> How do I make the line output c("f1", "f22") instead? (Actually,  
>> c(1,22)
>> would be even better).
>>
>
> strapply in gsubfn extracts matches based on content rather than
> delimiters.  See home page for more info http://gsubfn.googlecode.com
>
>> library(gsubfn)
>> x <- "f1=5,f22=3,"
>> strapply(x, "f\\d+")[[1]]
> [1] "f1"  "f22"

I read the request as for c(1,22) offered without quotes so t emeaning  
was obscure but perhaps this is what OP wanted:

as.numeric( strapply(x, "f(\\d+)=")[[1]] )
[1]  1 22
# or
 > as.numeric(unlist(strapply(x, "f(\\d+)=")))
[1]  1 22



>
> -- 
> GKX Group
> GKX Associates Inc.
> 1-877-GKX-GROUP
> ggrothendieck at gmail.com
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list