[R] evaluate one variable in an expression with two variables

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 9 01:52:16 CET 2012


On Wed, Feb 8, 2012 at 4:02 PM, moli <nirdong at gmail.com> wrote:
> I want to evaluate f with the mean=7
>
>  mean=7
>  f <-  expression(-(x-mean)^2/2)
> then get a new expression:
>
>  -(x-7)^2/2
> How could I do it? Thanks.
>

You can leave out the initial as.expression if you don't mind having a
call object instead:

> as.expression(do.call("substitute", list(f[[1]], list(mean = 7))))
expression(-(x - 7)^2/2)

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list