Hello, I'm not sure how to state my question in a technically accurate manner, so I'll use a short example to clarify my problem: Say I have a vector, vec<-c(1,2,3,2) I would like to be able to "reference" the vector by using the string containing the name of the vector - that is, I would like to know of some function which is able to convert the string "vec" into the vector vec itself. The purpose of this is to be able to do something like>somefunc("vec")==2[1] FALSE TRUE FALSE TRUE I hope it is clear from this what the problem is. It seems to me that the function desired is akin to a sort of "inverse" to the quote() function, but I have been unable to find such a function. I am using R 1.9.1 on Windows 2000 SP4. Does anyone have a suggestion of how to solve the problem? Thanks, Alexander
Hi Alexander, I'm not quite sure of what you want, but maybe this will help: vec <- c(1,2,3,2) get("vec")==2 Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Alexander Sokol" <alexandersokol at ofir.dk> To: <R-help at stat.math.ethz.ch> Sent: Thursday, November 04, 2004 3:00 PM Subject: [R] Conversion of strings to expressions> Hello, > > I'm not sure how to state my question in a technically accurate > manner, so > I'll use a short example to clarify my problem: > > Say I have a vector, > > vec<-c(1,2,3,2) > > I would like to be able to "reference" the vector by using the > string > containing the name of the vector - that is, I would like to know of > some > function which is able to convert the string "vec" into the vector > vec itself. > The purpose of this is to be able to do something like > >>somefunc("vec")==2 > [1] FALSE TRUE FALSE TRUE > > I hope it is clear from this what the problem is. It seems to me > that the > function desired is akin to a sort of "inverse" to the quote() > function, but I > have been unable to find such a function. > > I am using R 1.9.1 on Windows 2000 SP4. Does anyone have a > suggestion of how > to solve the problem? > > Thanks, > Alexander > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Alexander Sokol wrote:> Hello, > > I'm not sure how to state my question in a technically accurate manner, so > I'll use a short example to clarify my problem: > > Say I have a vector, > > vec<-c(1,2,3,2) > > I would like to be able to "reference" the vector by using the string > containing the name of the vector - that is, I would like to know of some > function which is able to convert the string "vec" into the vector vec itself. > The purpose of this is to be able to do something like > > >>somefunc("vec")==2 > > [1] FALSE TRUE FALSE TRUE > > I hope it is clear from this what the problem is. It seems to me that the > function desired is akin to a sort of "inverse" to the quote() function, but I > have been unable to find such a function. > > I am using R 1.9.1 on Windows 2000 SP4. Does anyone have a suggestion of how > to solve the problem? > > Thanks, > Alexander >I think you want ?get. This is FAQ 7.21. --sundar
On Thu, 4 Nov 2004, Alexander Sokol wrote:> Say I have a vector, > > vec<-c(1,2,3,2) > > I would like to be able to "reference" the vector by using the string > containing the name of the vector - that is, I would like to know of some > function which is able to convert the string "vec" into the vector vec itself. > The purpose of this is to be able to do something like > >> somefunc("vec")==2 > [1] FALSE TRUE FALSE TRUE >A) This is a FAQ B) Many people who think they want to do this are mistaken, and they actually want to, for example, work with a list, one of whose elements is called "vec". -thomas
Dear Alexander, You can use get("vec"). I hope this helps, John On Thu, 4 Nov 2004 15:00:21 +0100 Alexander Sokol <alexandersokol at ofir.dk> wrote:> Hello, > > I'm not sure how to state my question in a technically accurate > manner, so > I'll use a short example to clarify my problem: > > Say I have a vector, > > vec<-c(1,2,3,2) > > I would like to be able to "reference" the vector by using the string > > containing the name of the vector - that is, I would like to know of > some > function which is able to convert the string "vec" into the vector > vec itself. > The purpose of this is to be able to do something like > > >somefunc("vec")==2 > [1] FALSE TRUE FALSE TRUE > > I hope it is clear from this what the problem is. It seems to me that > the > function desired is akin to a sort of "inverse" to the quote() > function, but I > have been unable to find such a function. > > I am using R 1.9.1 on Windows 2000 SP4. Does anyone have a suggestion > of how > to solve the problem? > > Thanks, > Alexander > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
?get> vec<-c(1,2,3,2) > get("vec") == 2[1] FALSE TRUE FALSE TRUE Note that this is converting a string to an object, not to an expression: the latter is done by> parse(text="vec")expression(vec) On Thu, 4 Nov 2004, Alexander Sokol wrote:> I'm not sure how to state my question in a technically accurate manner, so > I'll use a short example to clarify my problem: > > Say I have a vector, > > vec<-c(1,2,3,2) > > I would like to be able to "reference" the vector by using the string > containing the name of the vector - that is, I would like to know of some > function which is able to convert the string "vec" into the vector vec itself. > The purpose of this is to be able to do something like > > >somefunc("vec")==2 > [1] FALSE TRUE FALSE TRUE > > I hope it is clear from this what the problem is. It seems to me that the > function desired is akin to a sort of "inverse" to the quote() function, but I > have been unable to find such a function. > > I am using R 1.9.1 on Windows 2000 SP4. Does anyone have a suggestion of how > to solve the problem?-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595