[R] evaluation of expressions

Andrew Gelman gelman at stat.columbia.edu
Tue May 2 17:12:53 CEST 2006


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




More information about the R-help mailing list