Re: [R] evaluation of expressions

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed 03 May 2006 - 01:28:37 EST

in this case you need, something like:

eval(parse(text = expr))

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


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

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help@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 Received on Wed May 03 01:31:33 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 03 May 2006 - 02:09:59 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.