[R] how to GREP out a string like this......THANKS.

William Dunlap wdunlap at tibco.com
Tue May 21 02:54:59 CEST 2013


You suggested
  > lapply( lines, function(l) strsplit(l, " // ")[[1]][2] )

strsplit is vectorized so the following is equivalent but simpler and quicker:
   lapply(strsplit(lines, " // "), function(x)x[2])

The OP probably wants a character vector, not a list so use sapply or vapply (safer
than sapply and a bit quicker).  Any of the following would do:
  vapply(strsplit(lines, " // "), `[`, 2, FUN.VALUE="")
  vapply(strsplit(lines, " // "), function(x)x[2], FUN.VALUE="")
  sapply(strsplit(lines, " // "), `[`, 2) # wrong answer if length(lines)==0


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of David Winsemius
> Sent: Monday, May 20, 2013 5:17 PM
> To: Hon Kit (Stephen) Wong
> Cc: r-help at r-project.org
> Subject: Re: [R] how to GREP out a string like this......THANKS.
> 
> 
> On May 20, 2013, at 4:45 PM, Hon Kit (Stephen) Wong wrote:
> 
> > Dear ALl,
> >
> > I hope you could help me out on this simple problem. I have many thousand lines like
> this:
> > NM_019397 // Egfl6 // EGF-like-domain, multiple 6 // X F5|X 71.5 cM // 54156
> >
> > I want to extract the string inside the first // //, in this case is Egf16.
> 
> > strsplit("NM_019397 // Egfl6 // EGF-like-domain, multiple 6 // X F5|X 71.5 cM //
> 54156", split=" // ")[[1]][2]
> [1] "Egfl6"
> 
> You can use;
> 
> lapply( lines, function(l) strsplit(l, " // ")[[1]][2] )
> 
> 
> >
> >
> > How do I apply grep function?
> 
> Well, grep is only going to give you a test and you want a replacement or extraction
> function. sub or gsub would be possibilities but they are greedy so its a bit more difficult
> to constrain their targeting to only the first  and second "//".
> 
> --
> 
> David Winsemius
> Alameda, CA, USA
> 
> ______________________________________________
> 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