michael watson (IAH-C)
2007-May-31 10:48 UTC
[R] A matrix with mixed character and numerical columns
Is it possible to have one? I have a data.frame with two character columns and 6 numerical columns. I converted to a matrix as I needed to use the col() and row() functions. However, if I convert the data.frame to a matrix, using as.matrix, the numerical columns get converted to characters, and that messes up some of the calculations. Do I really have to split it up into two matrices, one character and the other numerical, just so I can use the col() and row() functions? Are there equivalent functions for data.frames?
Hi Michael, I dont think it is possible. Please see first the definition of a dataframe and matrix ----- Original Message ---- From: michael watson (IAH-C) <michael.watson@bbsrc.ac.uk> To: r-help@stat.math.ethz.ch Sent: Thursday, May 31, 2007 4:18:11 PM Subject: [R] A matrix with mixed character and numerical columns Is it possible to have one? I have a data.frame with two character columns and 6 numerical columns. I converted to a matrix as I needed to use the col() and row() functions. However, if I convert the data.frame to a matrix, using as.matrix, the numerical columns get converted to characters, and that messes up some of the calculations. Do I really have to split it up into two matrices, one character and the other numerical, just so I can use the col() and row() functions? Are there equivalent functions for data.frames? ______________________________________________ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]]
Petr PIKAL
2007-May-31 11:57 UTC
[R] Odp: A matrix with mixed character and numerical columns
Hi r-help-bounces at stat.math.ethz.ch napsal dne 31.05.2007 12:48:11:> Is it possible to have one? > > I have a data.frame with two character columns and 6 numerical columns. > > I converted to a matrix as I needed to use the col() and row() > functions. > However, if I convert the data.frame to a matrix, using as.matrix, the > numerical columns get converted to characters, and that messes up some > of the calculations. > > Do I really have to split it up into two matrices, one character and the > other numerical, just so I can use the col() and row() functions? Are > there equivalent functions for data.frames?AFAIK I do not remember equivalent functions for data frame. If you just want column or row index you can use 1:dim(DF)[1] or 1:dim(DF)[2] for rows and columns if you want repeat these indexes row or columnwise use rrr<-rep(1:dim(DF)[1], dim(DF)[2]) matrix(rrr,dim(DF)[1], dim(DF)[2]) rrr<-rep(1:dim(DF)[2], dim(DF)[1]) matrix(rrr,dim(DF)[1], dim(DF)[2], byrow=T) Regards Petr> > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.