[R] How to point a column of dataframe by a "character"

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jul 28 15:23:41 CEST 2010


On Wed, Jul 28, 2010 at 8:59 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hi Tony,
>
> I am sure there are other ways, but I would create formula objects and
> then pass them to lm().  Here's an example:
>
> mydata <- data.frame(Y = 1:10, X1 = 11:20, X2 = 21:30)
>
> my.names <- names(mydata)[-1]
>
> for(i in my.names) {
>  my.formula <- formula(paste("Y ~ ", i, sep = ""))
>  my.lm <- lm(my.formula, data = mydata)
>  print(summary(my.lm))
> }
>

You might want to also replace the my.lm<- line above with:

   my.lm <- do.call("lm", list(my.formula, data = quote(mydata)))

so that the Call: line in the output comes out fully expanded.



More information about the R-help mailing list