Re: [R] gsub and multiple replacements

From: Hans-Jörg Bibiko <bibiko_at_eva.mpg.de>
Date: Mon, 02 Jun 2008 19:33:12 +0200

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



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. Received on Mon 02 Jun 2008 - 19:19:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 02 Jun 2008 - 20:32:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive