[R] How to see if row names of a dataframe are stored compactly

Hsiu-Khuern Tang hsiu-khuern.tang at hp.com
Sat Oct 14 01:38:47 CEST 2006


Reading the list of changes for R version 2.4.0, I was happy to see that the
row names of dataframes can be stored compactly (as the integer n when
row.names(df) is 1:n).

help(row.names) contains this paragraph:

    Row names of the form '1:n' for 'n > 2' are stored internally in a
    compact form, which might be seen by calling 'attributes' but never
    via 'row.names' or 'attr(x, "row.names")'.

I am unable to get attributes(x)$row.names to return just nrow(x).  Am I
misreading the documentation?  Does "might be seen" mean "possibly in some
future version of R" in this case?

> (x <- as.data.frame(matrix(1:9, nrow=3)))
  V1 V2 V3
1  1  4  7
2  2  5  8
3  3  6  9
> attributes(x)$row.names
[1] 1 2 3
> row.names(x) <- seq(len=nrow(x))
> attributes(x)$row.names
[1] 1 2 3

Best,
Hsiu-Khuern.



More information about the R-help mailing list