Hi, I'm new to R and looking for a way to use a variable to reference to an object. I'm plotting several graphs on top of each other and want to do this by a for loop. The field I want to graph have names like a1,a2,a3, ... I can't figure out how to get this working: It should look like this: lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j) but the "paste()" does not seem to work in this context. Any help? Kind regards, Kris -- ------------------------------------------------------------------------ http://perswww.kuleuven.ac.be/~u0027178/VCard/mycard.php?name=krisn ------------------------------------------------------------------------ Minds are like parachutes, they only work when open -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
get() is your friend; e.g. plot(get(paste("x",1, sep="")), get(paste("x", 2, sep=""))) Andy -----Original Message----- From: Kris Nackaerts [mailto:kris.nackaerts at agr.kuleuven.ac.be] Sent: Friday, November 22, 2002 11:22 AM To: r-help at stat.math.ethz.ch Subject: [R] Use of variables to reference to objects Hi, I'm new to R and looking for a way to use a variable to reference to an object. I'm plotting several graphs on top of each other and want to do this by a for loop. The field I want to graph have names like a1,a2,a3, ... I can't figure out how to get this working: It should look like this: lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j) but the "paste()" does not seem to work in this context. Any help? Kind regards, Kris -- ------------------------------------------------------------------------ http://perswww.kuleuven.ac.be/~u0027178/VCard/mycard.php?name=krisn ------------------------------------------------------------------------ Minds are like parachutes, they only work when open -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kris Nackaerts wrote:> > Hi, > > I'm new to R and looking for a way to use a variable to reference to an > object. > > I'm plotting several graphs on top of each other and want to do this by > a for loop. The field I want to graph have names like a1,a2,a3, ... > I can't figure out how to get this working: > > It should look like this: > lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j) > > but the "paste()" does not seem to work in this context. > > Any help?1. I suggest to generate a list "a", which can be indexed with a[[j]], so you don't need any sophisticated mechanism to reference to your objects. 2. Answer of your question: Use get(). Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 22 Nov 2002, Kris Nackaerts wrote:> Hi, > > I'm new to R and looking for a way to use a variable to reference to an > object. > > I'm plotting several graphs on top of each other and want to do this by > a for loop. The field I want to graph have names like a1,a2,a3, ... > I can't figure out how to get this working: > > It should look like this: > lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j) > > but the "paste()" does not seem to work in this context. >No, it wouldn't. That gives you the name of the variable, not the variable. Probably the simplest solution is either lines(spectral$Wavelength, spectral[[paste("a",j,sep="")]],col=j) but there's also lines(spectral$Wavelength,get(paste("spectral$a",j,sep="")),col=j) and various others -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
The "get" function is the usual answer to this sort of question, but is not right for this particular case. Actually, a simpler answer works. lines(spectral$Wavelength, spectral[[ paste("a", j, sep="") ]], col=j) Using "get" would want to get objects with names like "spectral$a1" rather than the "a1" component of "spectral". Patrick Burns patrick at burns-stat.com +44 (0) 208 525 0696 http://www.burns-stat.com/ (new home of S Poetry) Kris Nackaerts wrote:> Hi, > > I'm new to R and looking for a way to use a variable to reference to > an object. > > I'm plotting several graphs on top of each other and want to do this > by a for loop. The field I want to graph have names like a1,a2,a3, ... > I can't figure out how to get this working: > > It should look like this: > lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j) > > but the "paste()" does not seem to work in this context. > > Any help? > > Kind regards, > > Kris >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._