[R] convert matrix to dataframe with repeating row names

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Mon Oct 20 12:47:04 CEST 2008


> > The row names on a data frame should be unique.  You can try
> > as.data.frame(xx, row.names=FALSE) to convert zz to be a data frame. 
If
> > you need the row name information, add it as a column in the data 
frame,
> > e.g. mydataframe$rnames <- rownames(zz).  (Note to R-Core: the
> > documentation for as.data.frame doesn't mention the usage of
> > row.names=FALSE to ignore row names, but it seems to work 
consistently.
> > Does the help page for as.data.frame need updating?)
> 
> No.  row.names=FALSE is not intended to work, and did you check every 
> single as.data.frame() method?
> 
> It just so happens that for the matrix method invalid input for 
> 'row.names' results in setting default row names.  Other methods may 
> differ.

row.names=FALSE seems a natural way of supressing existing row names to 
me, since it corresponds nicely to using row.names=FALSE in write.csv. 
Currently it seems that if a matrix has duplicate row names, then 
converting it to be a data frame requires

rnames <- rownames(mymatrix)
rownames(mymatrix) <- NULL
as.data.frame(mymatrix)
rownames(mymatrix) <- rnames 

Ideally, three of these lines of code shouldn't really need to be there.

If you disagree that allowing row.names=FALSE is a good idea, or you don't 
want to change the function interface, then perhaps having as.data.frame 
check for duplicates and throwing a warning (rather than an error) would 
be preferable behaviour.  I do realise that there are dozens of 
as.data.frame methods, and the documentation does state that "Few of the 
methods check for duplicated row names", but it would be beneficial from a 
user standpoint.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list