[R] evaluating expressions with sub expressions

baptiste auguie baptiste.auguie at googlemail.com
Fri Jan 29 18:00:02 CET 2010


Hi,

Would this do as an alternative syntax?

g1 <- quote(1/Tm)
mat <- list(0, bquote(f1*s1*.(g1)))
vals <- data.frame(f1=1, s1=.5, Tm=2)

sapply(mat, eval, vals)

HTH,

baptiste


On 29 January 2010 17:51, Jennifer Young
<Jennifer.Young at math.mcmaster.ca> wrote:
> Hallo
>
> I'm having trouble figuring out how to evaluate an expression when one of
> the variables in the expression is defined separately as a sub expression.
> Here's a simplified example
>
> mat <- expression(0, f1*s1*g1)  # vector of formulae
> g1 <- expression(1/Tm)          # expansion of the definition of g1
> vals <- data.frame(f1=1, s1=.5, Tm=2) # one set of possible values for
> variables
>
> before adding this sub expression I was using the following to evaluate "mat"
>
> sapply(mat, eval, vals)
>
> Obviously I could manually substitute in 1/Tm for each g1 in the
> definition of "mat", but the actual expression vector is much longer, and
> the sub expression more complicated. Also, the subexpression is often
> adjusted for different scenarios.  Is there a simple way of changing this
> or redefining "mat" so that I can define "g1" like a macro to be used in
> the expression vector.
>
> Thanks!
> Jennifer
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list