Re: [R] retrieving object name

From: Prof Brian Ripley <>
Date: Tue 11 Jul 2006 - 00:15:14 EST

On Mon, 10 Jul 2006, Laurent Deniau wrote:

> I have a data frame with named columns and I would like to know if it is
> possible to retrieve a column name once selected:

Not really. df$col1 is a new object which does not know where it came from.

If you wanted to do this before selection, then <- function(df, col)

   struture(df[[col]], from=col)

would do the extraction and set an attribute to be consulted later.

> print(colnames(df)) # assumes to print "col1" "col2"
>$col1) # would like to print "col1"
>$col2) # would like to print "col2"
> So what the function should do?
> My aim is not to print the column name but to select some settings from
> the column name withing the function (i.e., while this
> function is applied to several columns of the list/data.frame. Actually,
> I solved the problem by providing an extra parameter like:
>$col1, "col1")
> but since I may have many of these columns/parameters combination, this
> is rather error prone and it would be much better if I could detect
> which columns of the data frame I am dealing with.
> Thanks.
> ld.

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Tue Jul 11 01:31:06 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 11 Jul 2006 - 04:16:21 EST.

Mailing list information is available at Please read the posting guide before posting to the list.