Question about turning strings into variable names in R
3
0
Entering edit mode
@jdelasherasedacuk-1189
Last seen 8.7 years ago
United Kingdom
Hi, I have a situation where I have a vector of variable names. If I call any of them, I get a text string (the name is surrounded by ""). How can I use that as a variable name again? For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). If I want to return the contents of a, I can't do just v[1], because that only shows "a"... Is there a simple way to indicate that "a" should be taken as the name of an object, and not as a string? If so, I haven't found it... thanks! Jose -- Dr. Jose I. de las Heras Email: J.delasHeras at ed.ac.uk The Wellcome Trust Centre for Cell Biology Phone: +44 (0)131 6513374 Institute for Cell & Molecular Biology Fax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK
• 1.1k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 1 day ago
United States
Hi Jose, J.delasHeras at ed.ac.uk wrote: > Hi, > > I have a situation where I have a vector of variable names. If I call > any of them, I get a text string (the name is surrounded by ""). How > can I use that as a variable name again? > > For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). > If I want to return the contents of a, I can't do just v[1], because > that only shows "a"... > Is there a simple way to indicate that "a" should be taken as the name > of an object, and not as a string? If so, I haven't found it... I think you want get() here. get(v[1]) Best, Jim > > thanks! > > Jose > -- James W. MacDonald University of Michigan Affymetrix and cDNA Microarray Core 1500 E Medical Center Drive Ann Arbor MI 48109 734-647-5623 ********************************************************** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues.
ADD COMMENT
0
Entering edit mode
Quoting "James W. MacDonald" <jmacdon at="" med.umich.edu="">: > Hi Jose, > > J.delasHeras at ed.ac.uk wrote: >> Hi, >> >> I have a situation where I have a vector of variable names. If I >> call any of them, I get a text string (the name is surrounded by >> ""). How can I use that as a variable name again? >> >> For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). >> If I want to return the contents of a, I can't do just v[1], >> because that only shows "a"... >> Is there a simple way to indicate that "a" should be taken as the >> name of an object, and not as a string? If so, I haven't found it... > > I think you want get() here. > > get(v[1]) > > Best, > > Jim Brilliant! That's it exactly! Thanks, Jim! Jose -- Dr. Jose I. de las Heras Email: J.delasHeras at ed.ac.uk The Wellcome Trust Centre for Cell Biology Phone: +44 (0)131 6513374 Institute for Cell & Molecular Biology Fax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK
ADD REPLY
0
Entering edit mode
@sean-davis-490
Last seen 3 months ago
United States
On Thursday 01 March 2007 07:22, J.delasHeras at ed.ac.uk wrote: > Hi, > > I have a situation where I have a vector of variable names. If I call > any of them, I get a text string (the name is surrounded by ""). How > can I use that as a variable name again? > > For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). > If I want to return the contents of a, I can't do just v[1], because > that only shows "a"... > Is there a simple way to indicate that "a" should be taken as the name > of an object, and not as a string? If so, I haven't found it... http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string- into-a-variable_003f However, you probably don't need (or want) to do this. Use a list instead. v <- list() v[['a']] <- a v[['b']] <- b ... Sean
ADD COMMENT
0
Entering edit mode
Thanks Sean and Steen, for alternative solutions. Great! I have used a list before for similar purposes, but I now wanted a more "direct" approach. 'get' and 'eval(parse())' is what I was looking for now. Thanks! Jose Quoting Sean Davis <sdavis2 at="" mail.nih.gov="">: > On Thursday 01 March 2007 07:22, J.delasHeras at ed.ac.uk wrote: >> Hi, >> >> I have a situation where I have a vector of variable names. If I call >> any of them, I get a text string (the name is surrounded by ""). How >> can I use that as a variable name again? >> >> For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). >> If I want to return the contents of a, I can't do just v[1], because >> that only shows "a"... >> Is there a simple way to indicate that "a" should be taken as the name >> of an object, and not as a string? If so, I haven't found it... > > http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a -string-into-a-variable_003f > > However, you probably don't need (or want) to do this. Use a list instead. > > v <- list() > v[['a']] <- a > v[['b']] <- b > ... > > Sean > > -- Dr. Jose I. de las Heras Email: J.delasHeras at ed.ac.uk The Wellcome Trust Centre for Cell Biology Phone: +44 (0)131 6513374 Institute for Cell & Molecular Biology Fax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK
ADD REPLY
0
Entering edit mode
@stkh-steen-krogsgaard-797
Last seen 9.7 years ago
eval(parse(text=v[1])) cheers Steen -----Original Message----- From: bioconductor-bounces@stat.math.ethz.ch [mailto:bioconductor-bounces at stat.math.ethz.ch] On Behalf Of J.delasHeras at ed.ac.uk Sent: 1. marts 2007 13:22 To: bioconductor at stat.math.ethz.ch Subject: [BioC] Question about turning strings into variable names in R Hi, I have a situation where I have a vector of variable names. If I call any of them, I get a text string (the name is surrounded by ""). How can I use that as a variable name again? For instance, I have objects a, b and c, and a vector v<-c("a","b","c"). If I want to return the contents of a, I can't do just v[1], because that only shows "a"... Is there a simple way to indicate that "a" should be taken as the name of an object, and not as a string? If so, I haven't found it... thanks! Jose -- Dr. Jose I. de las Heras Email: J.delasHeras at ed.ac.uk The Wellcome Trust Centre for Cell Biology Phone: +44 (0)131 6513374 Institute for Cell & Molecular Biology Fax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK _______________________________________________ Bioconductor mailing list Bioconductor at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/bioconductor Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD COMMENT

Login before adding your answer.

Traffic: 494 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6