[R] gsub and multiple replacements

Hans-Jörg Bibiko bibiko at eva.mpg.de
Mon Jun 2 19:33:12 CEST 2008


On 02.06.2008, at 17:27, Ng Stanley wrote:

> I would like to replace "AAAAA BBBBB" by "AAAAA-BBBBB" and "AAAAAA 
> (DD)" by
> "AAAAAA" using a single gsub. Is that possible besides using two  
> gsub ?
>

Could you be a bit more precisely?

If you are dealing with two fix strings then you can write

ifelse(theString == "AAAAA BBBBB", "AAAAA-BBBBB", "AAAAAA")

if not, one could find a regexp to solve that problem, but one could  
also use gsub in a cascade:

gsub('regexp1', 'replace1', gsub('regexp2', 'replace2', theString) )  
etc.

--Hans



More information about the R-help mailing list