ryszard.czerminski@pharma.novartis.com
2004-Mar-02 17:59 UTC
[R] row.names are dropped when extracting one column ?
Apparently row names are dropped when I extract single column from a data frame. Why this behaviour ?> y <- as.matrix(df[,1:2]); length(row.names(y))[1] 324> y <- as.matrix(df[,1:1]); length(row.names(y))[1] 0 Best regards, Ryszard
Marc Schwartz
2004-Mar-02 19:06 UTC
[R] row.names are dropped when extracting one column ?
On Tue, 2004-03-02 at 11:59, ryszard.czerminski at pharma.novartis.com wrote:> Apparently row names are dropped when I extract > single column from a data frame. Why this behaviour ? > > > y <- as.matrix(df[,1:2]); length(row.names(y)) > [1] 324 > > y <- as.matrix(df[,1:1]); length(row.names(y)) > [1] 0 > > Best regards, > > RyszardPlease see R-FAQ 7.7: "Why do my matrices lose dimensions?" HTH, Marc Schwartz
Prof Brian Ripley
2004-Mar-02 19:18 UTC
[R] row.names are dropped when extracting one column ?
On Tue, 2 Mar 2004 ryszard.czerminski at pharma.novartis.com wrote:> Apparently row names are dropped when I extract > single column from a data frame. Why this behaviour ? > > > y <- as.matrix(df[,1:2]); length(row.names(y)) > [1] 324 > > y <- as.matrix(df[,1:1]); length(row.names(y)) > [1] 0Why are you converting a subsetted data frame to a matrix? df[, 1:2] is a data frame, and df[, 1:1] is a single column (most likely a vector). The latter is not going to have row names, but it might have names. It happens that in S the row names are not copied across as names. row.names applies to a data frame, and not to a matrix, strictly. -- 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