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: print(colnames(df)) # assumes to print "col1" "col2" print.name(df$col1) # would like to print "col1" print.name(df$col2) # would like to print "col2" So what the print.name 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. print.name), while this function is applied to several columns of the list/data.frame. Actually, I solved the problem by providing an extra parameter like: print.name(df$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.
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.> print(colnames(df)) # assumes to print "col1" "col2" > print.name(df$col1) # would like to print "col1" > print.name(df$col2) # would like to print "col2" > > So what the print.name 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. print.name), while this > function is applied to several columns of the list/data.frame. Actually, > I solved the problem by providing an extra parameter like: > > print.name(df$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, 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
Use drop = FALSE. For example using builtin data frame BOD we can display the Time column with its heading: BOD[, "Time", drop = FALSE] On 7/10/06, Laurent Deniau <laurent.deniau at cern.ch> 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: > > print(colnames(df)) # assumes to print "col1" "col2" > print.name(df$col1) # would like to print "col1" > print.name(df$col2) # would like to print "col2" > > So what the print.name 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. print.name), while this > function is applied to several columns of the list/data.frame. Actually, > I solved the problem by providing an extra parameter like: > > print.name(df$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. > > ______________________________________________ > R-help at 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 >
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.