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

David Winsemius dwinsemius at comcast.net
Tue May 21 02:17:02 CEST 2013


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



More information about the R-help mailing list