Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove part of a string

Tags:

string

regex

r

How do I remove part of a string? For example in ATGAS_1121 I want to remove everything before _.

like image 769
Lisann Avatar asked Mar 14 '12 14:03

Lisann


2 Answers

Use regular expressions. In this case, you can use gsub:

gsub("^.*?_","_","ATGAS_1121") [1] "_1121" 

This regular expression matches the beginning of the string (^), any character (.) repeated zero or more times (*), and underscore (_). The ? makes the match "lazy" so that it only matches are far as the first underscore. That match is replaced with just an underscore. See ?regex for more details and references

like image 52
Joshua Ulrich Avatar answered Sep 18 '22 23:09

Joshua Ulrich


You can use a built-in for this, strsplit:

> s = "TGAS_1121" > s1 = unlist(strsplit(s, split='_', fixed=TRUE))[2] > s1      [1] "1121" 

strsplit returns both pieces of the string parsed on the split parameter as a list. That's probably not what you want, so wrap the call in unlist, then index that array so that only the second of the two elements in the vector are returned.

Finally, the fixed parameter should be set to TRUE to indicate that the split parameter is not a regular expression, but a literal matching character.

like image 41
doug Avatar answered Sep 21 '22 23:09

doug