Re: [R] A matrix with mixed character and numerical columns

From: Petr PIKAL <petr.pikal_at_precheza.cz>
Date: Thu, 31 May 2007 15:22:47 +0200


r-help-bounces_at_stat.math.ethz.ch napsal dne 31.05.2007 14:32:01:

> What I am trying to do is create an x-y plot from the numerical values,
> and the output of row() or col() gives me an excellent way of
> calculating an x- or y- co-ordinate, with the value in the data.frame
> being the other half of the pair.
>
> Thanks for the code, Petr - I'm sure you would agree, however, that it's
> a bit 'clumsy' (no fault of yours).
>
> Can we just adjust row() and col() for data.frames?
>
> col <- function (x, as.factor = FALSE)
> {
> if (is.data.frame(x)) {
> x <- as.matrix(x)
> }
> if (as.factor)
> factor(.Internal(col(x)), labels = colnames(x))
> else .Internal(col(x))
> }
>
> row <- function (x, as.factor = FALSE)
> {
> if (is.data.frame(x)) {
> x <- as.matrix(x)
> }
> if (as.factor)
> factor(.Internal(row(x)), labels = rownames(x))
> else .Internal(row(x))
> }
>
> Is there any reason why these won't work? Am I oversimplifying it?

Seems to me that both works. At least on data.frame I tried it.

Regards
Petr

>
> Mick
> -----Original Message-----
> From: Petr PIKAL [mailto:petr.pikal_at_precheza.cz]
> Sent: 31 May 2007 12:57
> To: michael watson (IAH-C)
> Cc: r-help_at_stat.math.ethz.ch
> Subject: Odp: [R] 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 guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help_at_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.



R-help_at_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. Received on Thu 31 May 2007 - 13:48:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 May 2007 - 16:31:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.