[R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Ravi S. Shankar
Hi R,

 

I have a matrix x with repeating row names. 

 dim(x)

[1] 862  19

 

zz-matrix(0,4,4)

rownames(zz)=c(a,a,b,b)

data.frame(zz) (?)

 

 

I need to use x in a linear regression

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

 

 

 sessionInfo()

R version 2.7.1 (2008-06-23) 

i386-pc-mingw32 

 

locale:

LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

 

attached base packages:

[1] stats graphics  grDevices utils datasets  methods   base


 

other attached packages:

[1] xlsReadWrite_1.3.2

 

Thanks in advance

Ravi

 

 

 

This e-mail may contain confidential and/or privileged i...{{dropped:13}}

__
R-help@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.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Prof Brian Ripley

On Mon, 20 Oct 2008, [EMAIL PROTECTED] 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@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,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Richard . Cotton
  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}}

__
R-help@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.


Re: [R] convert matrix to dataframe with repeating row names

2008-10-20 Thread Richard . Cotton
 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?)

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