Dear all, Suppose I have a very long expression e. Lets assume, for simplicity, that it is e = expression(u1+u2+u3) Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' expression, after replacement, should be:> eexpression(u1+x+1) My question is how to do the replacement? I have tried using:> e = parse(text=gsub("u2","x",e)) > e = parse(text=gsub("u3",1,e))Even though this works fine in this simple example, the use of parse when e is very long will fail since parse has a maximum line length and will cut my expressions. I need to keep mode(e)=expression since I will use e further in symbolic derivation and division. Any suggestions are most welcome. Thank you. Regards, Daniel Berg
Henrique Dallazuanna
2007-Mar-27 14:06 UTC
[R] Replacement in an expression - can't use parse()
Hi, perhaps you can use: ?chartr -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O<http://maps.google.com/maps?f=q&hl=en&q=Curitiba,+Brazil&layer=&ie=UTF8&z=18&ll=-25.448315,-49.276916&spn=0.002054,0.005407&t=k&om=1> On 27/03/07, Daniel Berg <daniel@danielberg.no> wrote:> > Dear all, > > Suppose I have a very long expression e. Lets assume, for simplicity, that > it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > > > e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > > > e = parse(text=gsub("u2","x",e)) > > e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel Berg > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
Gabor Grothendieck
2007-Mar-27 14:22 UTC
[R] Replacement in an expression - can't use parse()
Try substitute: e <- expression(u1 + u2 + u3) L <- list(u2 = as.name("x"), u3 = 1) as.expression(do.call(substitute, list(as.call(e), L))[[1]]) On 3/27/07, Daniel Berg <daniel at danielberg.no> wrote:> Dear all, > > Suppose I have a very long expression e. Lets assume, for simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > > > e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > > > e = parse(text=gsub("u2","x",e)) > > e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel Berg > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Sundar Dorai-Raj
2007-Mar-27 14:24 UTC
[R] Replacement in an expression - can't use parse()
Daniel Berg said the following on 3/27/2007 6:56 AM:> Dear all, > > Suppose I have a very long expression e. Lets assume, for simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > >> e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > >> e = parse(text=gsub("u2","x",e)) >> e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel Berg > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Take a look at ?substitute (or ?bquote) > as.expression(substitute(u1+u2+u3, list(u1 = quote(x)))) expression(x + u2 + u3) > as.expression(bquote(u1+u2+.(u3), list(u3 = 1))) expression(u1 + u2 + 1) HTH, --sundar
Dimitris Rizopoulos
2007-Mar-27 14:27 UTC
[R] Replacement in an expression - can't use parse()
you could try something like the following (untested): new.e <- eval(substitute(expression(u1+u2+u3), list(u2 = x, u3 = 1))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Daniel Berg" <daniel at danielberg.no> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, March 27, 2007 3:56 PM Subject: [R] Replacement in an expression - can't use parse()> Dear all, > > Suppose I have a very long expression e. Lets assume, for > simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > >> e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > >> e = parse(text=gsub("u2","x",e)) >> e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since > I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel Berg > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Christos Hatzis
2007-Mar-27 14:29 UTC
[R] Replacement in an expression - can't use parse()
A way to do this is through substitute: e1 <- substitute(expression(u1 + u2 + u3), list(u2=quote(x), u3=1)) But I am not sure whether you will run into similar limitations regarding the length of the expression to be substituted. -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Daniel Berg > Sent: Tuesday, March 27, 2007 9:57 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Replacement in an expression - can't use parse() > > Dear all, > > Suppose I have a very long expression e. Lets assume, for > simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > > > e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > > > e = parse(text=gsub("u2","x",e)) > > e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use > of parse when e is very long will fail since parse has a > maximum line length and will cut my expressions. I need to > keep mode(e)=expression since I will use e further in > symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel Berg > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >
Daniel Berg wrote:> Dear all, > > Suppose I have a very long expression e. Lets assume, for simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > > >> e >> > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > > >> e = parse(text=gsub("u2","x",e)) >> e = parse(text=gsub("u3",1,e)) >> > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. >The short answer is substitute(). However, this is not entirely trivial to apply if you have your expression already inside an expression() object. The easy thing to do is> substitute(u1+u2+u3, list(u2=quote(x),u3=1))u1 + x + 1 but notice that this "autoquotes" the first argument, so> substitute(e, list(u2=quote(x),u3=1))e which is pretty much useless. (Arguably it would have been a better design to avoid this feature and require substitute(quote(.....)....) for the former case.) The way around this is to add a further layer of substitute() to insert the value of e:>eval(substitute(substitute(call,list(u2=quote(x),u3=1)),list(call=e[[1]]))) u1 + x + 1 Notice that substitute will not go inside expression objects, so we need to extract the mode "call" object using e[[1]]. Also, the result is "call" not "expression". You may need an as.expression construct around the result to get exactly what you asked. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Tue, 2007-03-27 at 15:56 +0200, Daniel Berg wrote:> Dear all, > > Suppose I have a very long expression e. Lets assume, for simplicity, that it is > > e = expression(u1+u2+u3) > > Now I wish to replace u2 with x and u3 with 1. I.e. the 'new' > expression, after replacement, should be: > > > e > expression(u1+x+1) > > My question is how to do the replacement? > > I have tried using: > > > e = parse(text=gsub("u2","x",e)) > > e = parse(text=gsub("u3",1,e)) > > Even though this works fine in this simple example, the use of parse > when e is very long will fail since parse has a maximum line length > and will cut my expressions. I need to keep mode(e)=expression since I > will use e further in symbolic derivation and division. > > Any suggestions are most welcome. > > Thank you. > > Regards, > Daniel BergHere is one possibility, depending upon how complicated the substitutions end up being: e <- expression(u1+u2+u3) TMP <- unlist(strsplit(as.character(e), " "))> TMP[1] "u1" "+" "u2" "+" "u3" TMP <- gsub("u2", "x", TMP) TMP <- gsub("u3", "i", TMP)> TMP[1] "u1" "+" "x" "+" "i"> as.expression(paste(TMP, collapse = ""))expression("u1+x+i") HTH, Marc Schwartz