Hsiu-Khuern Tang
2006-Oct-13 23:38 UTC
[R] How to see if row names of a dataframe are stored compactly
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.
Gabor Grothendieck
2006-Oct-13 23:59 UTC
[R] How to see if row names of a dataframe are stored compactly
Try this:> class(attributes(x)$row.names)[1] "integer"> rownames(x) <- as.character(rownames(x)) > class(attributes(x)$row.names)[1] "character" On 10/13/06, Hsiu-Khuern Tang <hsiu-khuern.tang at hp.com> 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? 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. > > ______________________________________________ > 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. >
Prof Brian Ripley
2006-Oct-14 07:23 UTC
[R] How to see if row names of a dataframe are stored compactly
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 3But 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
Possibly Parallel Threads
- Notes on building a gcc toolchain for Rtools (but not multilib)
- Notes on building a gcc toolchain for Rtools (but not multilib)
- Notes on building a gcc toolchain for Rtools (but not multilib)
- Notes on building a gcc toolchain for Rtools (but not multilib)
- Notes on building a gcc toolchain for Rtools (but not multilib)