Re: [R] Annoyance in as.numeric

From: Bert Gunter <gunter.berton_at_gene.com>
Date: Mon, 16 Jun 2008 14:41:16 -0700

On Mon, Jun 16, 2008 at 4:22 PM, Alberto Monteiro wrote:

> Why does as.numeric convert matrices and arrays to vectors?
>

Because that's what it's documented to do!

" as.numeric is a generic function, but S3 methods must be written for as.double. It is identical to as.double (and as.real). "

And a check of the Help for as.double says:

"as.double attempts to coerce its argument to be of double type: like as.vector it strips attributes including names. (To ensure that an object is of double type without stripping attributes, use storage.mode)."

So, in particular, dim attributes of an array are stripped. To preserve them,do what the Help tells you to:

> x <- matrix(c("1","2","3","4"),2,2)
> x

     [,1] [,2]
[1,] "1" "3"
[2,] "2" "4"
> storage.mode(x) <- "double"
> x

     [,1] [,2]
[1,] 1 3
[2,] 2 4

Cheers,

Bert Gunter
Genentech Nonclinical Statistics



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 - 21:56:04 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 - 22:30:51 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