[R] names( d$columnname )

Charles C. Berry cberry at tajo.ucsd.edu
Mon Apr 23 05:41:45 CEST 2007

On Sun, 22 Apr 2007, ivo welch wrote:

> yes, this was what I was asking for.  I had the notion that one could
> tag almost anything with a name, and did not appreciate the scope for
> confusion.  thank you for the explanation.  regards, /ivo

You haven't told us why you want this behavior.

If what you want is a tag that will follow d$columnname around when you 
assign it to another object, you can add an attribute that has the tag in 


> df <- as.data.frame(diag(3))
> for (i in names(df)) attr( df[[ i ]], "my.name" ) <- i
> df.v1 <- df$V1
> attr(df.v1,"my.name")
[1] "V1"

> On 4/22/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 4/22/2007 5:46 PM, ivo welch wrote:
>>> dear R wizards --- would it make sense for names(d$columnname) to be
>>> "columnname"?  I can preserve the columnname through x=subset(dataset,
>>> select="columnname"), of course, but it would seem that x=d$columnname
>>>  could also do this.  No?  Sincerely,  /iaw
>> If I understand what you're asking, I don't think so.
>> d$columnname
>> extracts an element of the list d.  It's typically some sort of vector,
>> and vectors don't know what their own name is.
>> names(d$columnname)
>> looks for a names attribute on that vector.  If it doesn't have one,
>> you'll get NULL.
>> There are plenty of functions in R which depend on the form of the
>> argument passed to them, not just its value, but we shouldn't add more
>> without a very good reason.  The change you ask for would mess up the
>> following calculation:
>> > x <- 1:26
>> > names(x) <- letters
>> > d <- list(columnname=x)
>> > names(d$columnname)
>>   [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
>> "q" "r"
>> [19] "s" "t" "u" "v" "w" "x" "y" "z"
>> Duncan Murdoch
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0901

More information about the R-help mailing list