[R] row.names are dropped when extracting one column ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Mar 2 20:18:04 CET 2004


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

Why 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




More information about the R-help mailing list