[R] String replacement in an expression

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 28 18:07:17 CEST 2009


Try matching on word boundaries as well:

> gsub("\\bCl\\b","(Cl+beta)",as.character(form))
[1] "((Cl+beta) - (V * ka)) + (V * Vm * exp(-(Clm/Vm) * t))"

See ?regexp

On Thu, May 28, 2009 at 11:41 AM, Caroline Bazzoli
<caroline.bazzoli at inserm.fr> wrote:
> Dear R-experts,
>
> I need to replace in an expression the character "Cl" by "Cl+beta"
>
> But in the following case:
>
> form<-expression((Cl-(V *ka)  ) +(V   *Vm   *exp(-(Clm/Vm)   *t)))
>
> gsub("Cl","(Cl+beta)",as.character(form))
>
> We obtain:
>
> [1] "((Cl+beta) - (V * ka)) + (V * Vm * exp(-((Cl+beta)m/Vm) * t))"
>
>
> the character "Clm" has been also replaced.
>
>
> How could I avoid this unwanted replacement ?
>
>
> Thank you in advance for any help.
>
> --
> ---------------------------------
> Caroline BAZZOLI
>
>
> INSERM U738 - Université PARIS 7
> UFR de Medecine - Site Bichat
> 16 rue Henri Huchard
> 75018 PARIS, FRANCE
> email: caroline.bazzoli at inserm.fr
>
> www.biostat.fr PFIM: www.pfim.biostat.fr
>
> ______________________________________________
> 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