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

______________________________________________
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.

Reply via email to