On Fri, Jan 16, 2015 at 3:16 AM, philippe massicotte
<pmassico...@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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to