[R] loop through variable names

Jacob Wegelin jacobwegelin at fastmail.fm
Thu Nov 12 00:49:43 CET 2009


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.




More information about the R-help mailing list