Dear all Is there an easier way to retrieve the name of an object? For example,> tmp <- 1:10 > as.character(quote(tmp))[1] "tmp"> as.character(quote(mtcars$cyl))[1] "$" "mtcars" "cyl"> as.character(quote(mtcars$cyl))[3][1] "cyl" The last call more than anything seems a hack. Is there a better way? Thank you Liviu
I am not sure what you mean by a "hack"? Can you elaborate further, give details on the problem you are trying to solve. Does this work as a "lesser hack":> tail(as.character(quote(mtcars$cyl)),1)[1] "cyl"> tail(as.character(quote(cyl)),1)[1] "cyl">On Wed, Aug 4, 2010 at 7:47 AM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all > Is there an easier way to retrieve the name of an object? For example, >> tmp <- 1:10 >> as.character(quote(tmp)) > [1] "tmp" >> as.character(quote(mtcars$cyl)) > [1] "$" ? ? ?"mtcars" "cyl" >> as.character(quote(mtcars$cyl))[3] > [1] "cyl" > > The last call more than anything seems a hack. Is there a better way? > > Thank you > Liviu > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Tryu this: deparse(substitute(mtcars)) On Wed, Aug 4, 2010 at 8:47 AM, Liviu Andronic <landronimirc@gmail.com>wrote:> Dear all > Is there an easier way to retrieve the name of an object? For example, > > tmp <- 1:10 > > as.character(quote(tmp)) > [1] "tmp" > > as.character(quote(mtcars$cyl)) > [1] "$" "mtcars" "cyl" > > as.character(quote(mtcars$cyl))[3] > [1] "cyl" > > The last call more than anything seems a hack. Is there a better way? > > Thank you > Liviu > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 04/08/2010 7:47 AM, Liviu Andronic wrote:> Dear all > Is there an easier way to retrieve the name of an object? For example, > > tmp <- 1:10 > > as.character(quote(tmp)) > [1] "tmp" > > as.character(quote(mtcars$cyl)) > [1] "$" "mtcars" "cyl" > > as.character(quote(mtcars$cyl))[3] > [1] "cyl" > > The last call more than anything seems a hack. Is there a better way?I don't understand what you want. "cyl" is not the name of an object; it's the name of a component of the mtcars object. If you know that, you can simply use names(mtcars) to get all the names, and names(mtcars)[2] to get "cyl". Perhaps you thought the result of the expression mtcars$cyl was an object whose name was "cyl"? It's not. Unless you assign it to something, it's an anonymous object. As other have pointed out, in a function you can use substitute(arg) to retrieve the expression passed as arg, and deparse(substitute(arg)) to turn it into a string that's suitable for using as a label. But that's not the name of the object. The name of the object in that case is "arg". Duncan Murdoch