[R] Extract a 'data.frame$column' column name

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon May 3 13:40:15 CEST 2010

> as.character(quote(foo$Bar)[[3]])
[1] "Bar"

Hint: this is nothing to do with data frames ($ applies to lists). $ 
is an operator, so foo$Bar is a call.  quote() stops it being 
evaluated, [[3]] selects the third of the elements (which are $, foo, 
Bar) and as.character turns the name into a character string.

On Mon, 3 May 2010, adrien Penac wrote:

> Hello,
>  I can't find how to get de column name from a data.frame dollar reference.
> To make it simple, I'd like to obtain "Bar" from a "foo$Bar" notation.
> I've tried col.names(foo$Bar), names(foo$Bar) and so on without sucess.
> Regards
> Blaise

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

More information about the R-help mailing list