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

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Mar 27 16:27:49 CEST 2007


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



More information about the R-help mailing list