Re: [R] Replacement in an expression - can't use parse()

From: Christos Hatzis <christos_at_nuverabio.com>
Date: Tue 27 Mar 2007 - 14:29:36 GMT

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@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Daniel Berg
> Sent: Tuesday, March 27, 2007 9:57 AM
> To: r-help@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@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.
>
>



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. Received on Wed Mar 28 00:59:26 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 27 Mar 2007 - 15:30:33 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.