[R] Extracting the name of an object

Duncan Murdoch murdoch at stats.uwo.ca
Sat Jun 13 02:17:19 CEST 2009


On 12/06/2009 7:53 PM, Payam Minoofar wrote:
> With apologies if I missed the answer in the response given to my previous question.
> 
> How do I extract the name of an object and assign it to a string variable?
> 
> For example, I have a dataframe named comnoglyc
> 
> How do I assign this name to a variable dfname such that print(dfname) returns "comnoglyc"?

Objects don't know their own names, so this isn't possible in general.
However, arguments know what expression was passed as their value, which 
is somewhat similar. For example, the function

 > f <- function(x) deparse(substitute(x))

uses substitute() to get the expression passed as x and then converts it 
to a string using deparse:

 > f(comnoglyc)
[1] "comnoglyc"

Duncan Murdoch




More information about the R-help mailing list