[R] Evaluating a formula

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 16 13:56:15 CET 2015


On Fri, Jan 16, 2015 at 3:16 AM, philippe massicotte
<pmassicotte at hotmail.com> wrote:
> Hi all.
>
> How we evaluate a formula in R?
>
> Ex.:
>
> params <- list(a = 2, b = 3)
> x <- seq(1,10, length.out = 100)
>
> func1 <- as.formula("y ~ a*x^2 + b*x")
>
> ##How to evaluate func1 using x and the params list
> ???
>
>
> Thank you in advance,
> Phil


Remove the lhs of the formula giving fo; then use fn$ from gsubfn to
turn fo into a function, func, and call it using do.call.

library(gsubfn)
fo <- formula(sub(".*~", "~", deparse(func1)))
func <- fn$identity(fo)
do.call(func, c(list(x = x), params))



-- 
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