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.
______________________________________________
[email protected] 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.