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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Oct 14 09:23:37 CEST 2006


On Fri, 13 Oct 2006, Hsiu-Khuern Tang wrote:

> 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?

Definitely.  It does not say the 'compact form' is 'just nrow(x)', does 
it?  (It is not.)

> Does "might be seen" mean "possibly in some
> future version of R" in this case?

It is not intended that the user ever sees the compact form from R code 
(it can be seen from C code and also by deparsing), but there were 
circumstances under which attributes() would show it (but no longer, I 
believe).

>> (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

But see what dump() gives you on that object.

-- 
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



More information about the R-help mailing list