Re: [R] Annoyance in as.numeric

From: Taylor, Z Todd <todd.taylor_at_pnl.gov>
Date: Mon, 16 Jun 2008 14:50:48 -0700

I generally do something like

    storage.mode(my.matrix) <- "double"

--Todd

-- 
Why do you recite at a play and play at a recital?

 


> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Gavin Simpson
> Sent: Monday, June 16, 2008 2:31 PM
> To: markleeds_at_verizon.net
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Annoyance in as.numeric
>
> On Mon, 2008-06-16 at 15:35 -0500, markleeds_at_verizon.net wrote:
> > hi: you can do below but i don't know if it's worth it ?
> >
> > newx <- data.matrix(data.frame(x))
> > print(newx)
>
> That doesn't work Mark:
>
> > str(data.frame(x))
> 'data.frame': 2 obs. of 2 variables:
> $ X1: Factor w/ 2 levels "1","2": 1 2
> $ X2: Factor w/ 2 levels "3","4": 1 2
> > data.matrix(data.frame(x))
> X1 X2
> [1,] 1 1
> [2,] 2 2
>
> Which is as per documentation - the data.frame() coerces to
> factors and
> data.matrix represents factor by their internal values.
>
> One "solution" might be to save the dims and then reapply:
>
> > mat
> [,1] [,2]
> [1,] "1" "3"
> [2,] "2" "4"
> > dims <- dim(mat)
> > mat2 <- as.numeric(mat)
> > dim(mat) <- dims
> > mat2
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> You could wrap this in a function:
>
> asMatrix <- function(x, ...) {
> dims <- dim(x)
> x <- as.double(x)
> dim(x) <- dims
> return(x)
> }
>
> It would be logical to define a method for as.double for
> matrix objects.
> However objects of class "matrix" are not objects in the sense of
> isObject() and hence method dispatch will not work in this case.
>
> HTH
>
> G
>
>
> >
> >
> > On Mon, Jun 16, 2008 at 4:22 PM, Alberto Monteiro wrote:
> >
> > > Why does as.numeric convert matrices and arrays to vectors?
> > >
> > > as.numeric(matrix(c("1", "2", "3", "4"), 2, 2))
> > > [1] 1 2 3 4
> > >
> > > I could only figure out ugly ways to bypass this, like:
> > >
> > > x <- matrix(c("1", "2", "3", "4"), 2, 2)
> > > array(as.numeric(x), dim = dim(x), dimnames = dimnames(x))
> > >
> > > Alberto Monteiro
> > >
> > > ______________________________________________
> > > 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.
> >
> > ______________________________________________
> > 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.
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Dr. Gavin Simpson [t] +44 (0)20 7679 0522
> ECRC, UCL Geography, [f] +44 (0)20 7679 0565
> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
> UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
> ______________________________________________
> 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.
>
______________________________________________ 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.
Received on Mon 16 Jun 2008 - 22:14:45 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 Mon 16 Jun 2008 - 23:31:29 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.

list of date sections of archive