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
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 >
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 ----- Original Message ----- From: "Andrew Gelman" <gelman at stat.columbia.edu> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, May 02, 2006 5:12 PM Subject: [R] evaluation of expressions> 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm