[R] lm: eval(parse(text=)) works on one side y/x but not on both?

Matthieu Stigler matthieu.stigler at gmail.com
Sun Nov 29 14:49:27 CET 2009


Hi

My goal is to do a (multiple) regression, just knowing that my Y 
variables will be the say k first variables of a matrix/data frame. I 
thought I should do it with eval(parse)) but encounter a strange problem.

See:
lm(y~.-y, data=freeny) #that's what I want to do in the one equation case
#Problem is I don't know name of the variable... only that it is the 
first one...
#so idea is to just take first name
a<-colnames(freeny)
#and then use eval(parse(text=a[1]))

#it works if I replace y on either the left or right side:
lm(eval(parse(text=a[1]))~.-y, data=freeny) #does the same
lm(y~.-eval(parse(text=a[1])), data=freeny)

#but not if I do this call twice:
lm(eval(parse(text=a[1]))~.-eval(parse(text=a[1])), data=freeny)
#variable I wanted to remove (y) ist still there

Do you understand why I can call eval(parse) only once? Should I try a 
update workaround? Or have idea of any other solution? Maybe there is 
something much simpler I'm missing:-(

Thanks a lot!!!

Matthieu Stigler




More information about the R-help mailing list