[R] evaluating expressions with sub expressions

Jennifer Young Jennifer.Young at math.mcmaster.ca
Fri Jan 29 18:19:25 CET 2010


Hmm

I *think* this will work, but may break in a further sub routine.
It certainly works in this example, but my expression vector is used in
many scenarios and it will take a while to check them all.

For instance, I take the derivative of each element with respect to each
variable using

sapply(mat, deriv, names(vals))

This bit seems to still work, but I'd welcome a solution that doesn't
change the structure of the expression vector to a list, just in case.

Thanks for this solution.

> 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