[R] convert matrix to dataframe with repeating row names

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 20 12:03:14 CEST 2008


On Mon, 20 Oct 2008, Richard.Cotton at hsl.gov.uk wrote:

>> I have a matrix x with repeating row names.
>
>> zz<-matrix(0,4,4)
>>
>> rownames(zz)=c("a","a","b","b")
>>
>> data.frame(zz) (?)
>
> 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.

>
>> lm(as.formula(paste("final_dat[,5]~",paste(colnames(x),collapse="+"))),x
>> )
>>
>> this gives me a error
>
>> Error in model.frame.default(formula =
>> as.formula(paste("final_dat[,5]~",  :
>>
>>   'data' must be a data.frame, not a matrix or an array
>
> I suspect that if you try class(x), it will be a matrix, not the requisite
> data frame.
>
> Regards,
> Richie.
>
> Mathematical Sciences Unit
> HSL
>
>
> ------------------------------------------------------------------------
> ATTENTION:
>
> This message contains privileged and confidential inform...{{dropped:20}}
>
> ______________________________________________
> R-help at r-project.org 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.
>

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