[R] loop through variable names

Gabor Grothendieck ggrothendieck at gmail.com
Thu Nov 12 01:40:35 CET 2009


Try this:

lm(environmental[c("ToyOutcome", thisvar)])

or

lm(ToyOutcome ~., environmental[c("ToyOutcome", thisvar)])


On Wed, Nov 11, 2009 at 6:49 PM, Jacob Wegelin <jacobwegelin at fastmail.fm> wrote:
>
> Often I perform the same task on a series of variables in a dataframe, by
> looping through a character vector that holds the names and using paste(),
> eval(), and parse() inside the loop.
>
> For instance:
>
> thesevars<-names(environmental)
> environmental$ToyOutcome<-rnorm(nrow(environmental))
>
> tableOfResults<-data.frame(var=thesevars)
> tableOfResults$Beta<- NA
> rownames(tableOfResults)<-thesevars
>
> for( thisvar in thesevars) {
>        thiscommand<- paste("thislm <- lm( ToyOutcome ~ ", thisvar, ",
> data=environmental)")
>        eval(parse(text=thiscommand))
>        tableOfResults[thisvar, "Beta"] <- coef(thislm)[thisvar]
> }
>
> print(tableOfResults)
>
> Note that it's not always as simple a task as in this example. Within the
> loop, I might first figure out whether the variable is continuous or
> categorical, then perform an operation depending on its type--maybe lm() for
> continuous but wilcox.test() for dichotomous.
>
> But the use of paste(), eval(), and parse() seems awkward.  Is there a more
> elegant way to approach this?
>
> Thanks
>
> Jacob A. Wegelin
> Department of Biostatistics
> Virginia Commonwealth University
> Richmond VA 23298-0032
> U.S.A.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list