[R] evaluation of expressions
Gabor Grothendieck
ggrothendieck at gmail.com
Tue May 2 17:20:37 CEST 2006
Try this:
e <- expression(glm(y ~ age))
eval(e)
or this:
chr <- "glm(y ~ age)"
eval(parse(text = chr))
On 5/2/06, Andrew Gelman <gelman at stat.columbia.edu> wrote:
> Hi, all. I'm trying to automate some regression operations in R but am
> confused about how to evaluate expressoins that are expressed as
> character strings. For example:
>
> y <- ifelse (rnorm(10)>0, 1, 0)
> sex <- rnorm(10)
> age <- rnorm(10)
> test <- as.data.frame (cbind (y, sex, age))
>
> # this works fine:
> glm (y ~ sex + I(age^2), data=test, family=binomial(link="logit"),
> subset=age<1)
>
> # but now I want to do it in two steps:
> expr <- 'glm (y ~ sex + I(age^2), data=test,
> family=binomial(link="logit"), subset=age<1)'
>
> Given "expr", defined above, how can I evaluate it? I played around
> with eval() and as.expression() but can't figure it out.
>
> Thanks.
> Andrew
>
> --
> Andrew Gelman
> Professor, Department of Statistics
> Professor, Department of Political Science
> gelman at stat.columbia.edu
> www.stat.columbia.edu/~gelman
>
> Statistics department office:
> Social Work Bldg (Amsterdam Ave at 122 St), Room 1016
> 212-851-2142
> Political Science department office:
> International Affairs Bldg (Amsterdam Ave at 118 St), Room 731
> 212-854-7075
>
> Mailing address:
> 1255 Amsterdam Ave, Room 1016
> Columbia University
> New York, NY 10027-5904
> 212-851-2142
> (fax) 212-851-2164
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list