[Rd] data.frame row.names=NULL with named vector

Martin Morgan mtmorgan at fhcrc.org
Sun Oct 14 20:40:25 CEST 2007


A minor issue with data.frame is a change introduced leading to R
2.5.0, where row.names=NULL (i.e., do not name rows) is not honored
with a named vector

> x <- letters[1:3]
> names(x) <- x
> data.frame(x, row.names=NULL)
  x
a a
b b
c c

and a slightly more subtle example

> data.frame(y=1:3, x, row.names=NULL)
  y x
a 1 a
b 2 b
c 3 c

A 2.5.0 news entry says

    o	data.frame() ignored 'row.names' for 0-column data frames, and
	no longer treats an explicit row.names=NULL differently from
	the default value.

The relevant section of ?data.frame however says

     If 'row.names' was supplied as 'NULL' or no suitable component
     was found the row names are the integer sequence starting at one

This is not consistent with the current behavior, and is unchanged
from the pre 2.5.0 version. The workaround is distinctly hackish

> df <- data.frame(x)
> row.names(df) <- NULL

> sessionInfo()
R version 2.7.0 Under development (unstable) (2007-10-13 r43163) 
x86_64-unknown-linux-gnu 

Martin



More information about the R-devel mailing list