[R] String replacement in an expression

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu May 28 21:30:58 CEST 2009


Caroline Bazzoli 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 ?

try '\\bCl\\b' as the pattern, which says 'match Cl as a separate word'.

vQ




More information about the R-help mailing list