Re: [R] retrieving object name

From: Laurent Deniau <laurent.deniau_at_cern.ch>
Date: Tue 11 Jul 2006 - 01:19:07 EST

Prof Brian Ripley wrote:
> 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
>
> print.name <- function(df, col)
> struture(df[[col]], from=col)
>
> would do the extraction and set an attribute to be consulted later.

This is an appropriate solution since I build the list myself (by splitting the data frame following some factors) but I do not call the function myself (e.g. print.name). Here is the final steps of the function which read and set the data.frame/list:

magnet.read.rt <- function (filename) {

   # ...

   # convert to list by magnet states
   MG <- split(MG,MG$magnet_state)

   # set state attribute for automatic settings    for(col in names(MG))
     MG[[col]] <- structure(MG[[col]], state=col)

   invisible(MG)
}

and an example of its use:

.wc.scl <- function (MG) {

   c(CC=0.85, CM=1.0)[attr(MG,"state")]
}

wc.offset <- function (CM,CR) {

   scl <- .wc.scl(CM)
   #...
}

MB <- magnet.read.rt("magnet.dat")
CC.INJ <- wc.offset(MB$CC,MB$INJ)

Thanks.

a+, ld.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 11 01:21:47 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 - 02:15:59 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.