On Feb 24, 2010, at 8:18 PM, Jon Erik Ween wrote:
Friends
I can't quite find a direct answer to this question from the lists,
so here goes:
I have several dataframes, 200+ columns 2000+ rows. I wish to script
some operations to perform on some of the variables (columns) in the
data frames not knowing what the column number is, hence have to
refer by name. I have variable names in a text file "varlist". So,
something like this:
for (i in 1:length(varlist)){
j<-varlist[i]
v<-mean(Dataset[[j]])
print(v)
}
Without a data example this is untested guesswork and may fail if
varlist is not a vector or something that can be coerced to a vector.
Might help to look at str(varlist), ... anyway ...
for (i in varlist){
v<-mean(Dataset[ , i])
print(v)
}
If it doesn't work, then pay close attention to the error messages.
--
David
Now, if I force it
j<-"Var1"
v<-mean(Dataset[[j]])
print(v)
then it works, but not if i read the varlist as above.
Looking at "j" I get:
print(j)
V1
1 Var1
Right. You are supplying a list as an index.
Hence there is a lot of other stuff read into "j" that confuses
"mean".
I think it is confusing "[[", but it's really more guesswork because
you did not supply a reproducible example.
I can't figure out how to just get the value of the variable and
nothing else. I've tried space separated, comma separated, tab
separated lists and all give the same error. I've tried get(),
parse()... no go.
Any suggestions?
Thanks a lot
Jon
Soli Deo Gloria
Jon Erik Ween, MD, MS
Scientist, Kunin-Lunenfeld Applied Research Unit
Director, Stroke Clinic, Brain Health Clinic, Baycrest Centre
Assistant Professor, Dept. of Medicine, Div. of Neurology
University of Toronto Faculty of Medicine
Kimel Family Building, 6th Floor, Room 644
Baycrest Centre
3560 Bathurst Street
Toronto, Ontario M6A 2E1
Canada
Phone: 416-785-2500 x3648
Fax: 416-785-2484
Email: jw...@klaru-baycrest.on.ca
Confidential: This communication and any attachment(s) may contain
confidential or privileged information and is intended solely for
the address(es) or the entity representing the recipient(s). If you
have received this information in error, you are hereby advised to
destroy the document and any attachment(s), make no copies of same
and inform the sender immediately of the error. Any unauthorized use
or disclosure of this information is strictly prohibited.
[[alternative HTML version deleted]]
______________________________________________
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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
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.