[Rd] R-devel: rownames of a data.frame
Tony Plate
tplate at acm.org
Wed Dec 6 18:23:18 CET 2006
Martin Maechler wrote:
> Gregor Gorjanc <gregor.gorjanc <at> bfro.uni-lj.si> writes:
>
>
>>Hello!
>>
>>Data.frames have new rownames funcionality, however in use of colnames<-
>>in R-devel "changes" this. Here is the example:
>>
>>
>>>df1 <- data.frame(letters[1:5])
>>>attributes(df1)
>>
>>$names
>>[1] "letters.1.5."
>>
>>$row.names
>>[1] 1 2 3 4 5
>>
>>$class
>>[1] "data.frame"
>>
>>
>>>colnames(df1) <- "bla"
>>>attributes(df1)
>>
>>$names
>>[1] "bla"
>>
>>$row.names
>>[1] "1" "2" "3" "4" "5"
>>
>>$class
>>[1] "data.frame"
>>
>>?rownames/colnames help page (R-devel) says that value is coerced to
>>character, but why are rownames coerced to character if I assign
>>colnames to a data.frame?
>>
>
>
> because colnames() and rownames() work via dimnames(), and so do
> "colnames<-" and "rownames<-" ---
> I think it would be unwise to change this, since it's well documented property
> of these functions.
Where? -- the documentation for "colnames<-" on data frames doesn't seem
particularly specific about exactly how the replacement will be done.
And I don't see any mention of "colnames" in "An Introduction to R".
Not changing attr(,"row.names") when colnames are set seems a desirable
property. Allowing colnames<- to change attr(,"row.names") damages the
nice new feature of data frames that allows them to avoid using a lot of
storage on row names. It doesn't look like making "colnames<-" work via
"names<-" for dataframes would be particularly contradictory to the
documentation.
(Also, the documentation is no longer correct for rownames<- for data
frames -- it says the value must be character data.)
For reference, here's the Details section of ?colnames as it relates to
replacement "methods" (these functions don't appear to be generics...?):
> The replacement methods for arrays/matrices coerce vector
> and factor values of value to character, but do not dispatch
> methods for as.character.
>
> For a data frame, value for rownames should be a character
> vector of non-duplicated and non-missing names (this is
> enforced), and for colnames a character vector of
> (preferably) unique syntactically-valid names. In both
> cases, value will be coerced by as.character.
(taken from
https://svn.r-project.org/R/trunk/src/library/base/man/colnames.Rd)
-- Tony Plate
>
> As you noted yourself, using names() and "names<-"
> i.e.,
> names(df1) <- "bla"
> in the example above, is the recommended way for data frames and does not change
> the rownames to character.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list