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
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. >
> From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Caroline Bazzoli > Sent: Thursday, May 28, 2009 8:41 AM > To: r-help at r-project.org > Subject: [R] String replacement in an expression > > 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 ?I like to use substitute(), which works with the expression as an expression, instead of converting it to a string, changing it, and then parsing the result to make the new expression. E.g., > form<-expression((Cl-(V *ka) ) +(V *Vm *exp(-(Clm/Vm) *t))) > do.call("substitute", list(form[[1]], list(Cl=Quote(Cl+beta)))) (Cl + beta - (V * ka)) + (V * Vm * exp(-(Clm/Vm) * t)) Note how it adds parentheses where appropriate: > do.call("substitute", list(form[[1]], list(Cl=Quote(Cl+beta), Clm=Quote(Clm+gamma)))) (Cl + beta - (V * ka)) + (V * Vm * exp(-((Clm + gamma)/Vm) * t)) The do.call() is needed in this case because substitute() does not evaluate its first argument and the do.call takes care of evaluating the variable 'form' so substitute sees its value. (S+'s substitute() has an evaluator=FALSE/TRUE argument to let you avoid the do.call().) R's substitute() doesn't seem to go into expression objects, hence I passed it the first element of the expression and it returned the converted first element. You could wrap the output with as.expression if you really wanted the expression object output. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> > 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. >
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