Hello,
Jon Erik Ween wrote:
Hi
I would like to write a script that reads a list of variable names. These variable names
are some of the column headers in a data.frame. Then I want do a for-loop to execute
various operations on the specified variables in the data.frame, but can't figure out how
to do the necessary variable substitution. In bash (or C) I would use "$var",
but there seems to be no equivalent in R. The data.frame has 300 columns and 2500 rows.
Not all columns are continuous variables, some are factors, descriptors, etc., so I use
the varlist to pick which columns I want to analyze.
#Example script
varlist<-read.table(/path/to/varlist)
for (i in 1:length(varlist)){
res<-mean(Dataset$SOMETHINGHERE_i )
write(res) somewhere
}
In your script, perhaps
res <- mean(Dataset[[varlist[i]]])
would work.
Better might be:
colMeans(Dataset[varlist])
or if your actual function is not "mean":
sapply(Dataset[varlist], function)
where "function" is whatever you want (e.g., mean, sd, ...)
and do avoid the "varlist" problem completely, say you only want to run
it on numeric variables
sapply(Dataset[sapply(Dataset, is.numeric)], function)
None of this is tested, but the ideas should work.
______________________________________________
R-help@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.