R-alpha: as.numeric

Martin Maechler Martin Maechler <maechler@stat.math.ethz.ch>
Wed, 9 Apr 97 08:34:29 +0200


>>>>> "Thomas" == Thomas Lumley <thomas@biostat.washington.edu> writes:

    Thomas> as.numeric() turns a logical matrix into a numeric
    Thomas> vector. Shouldn't it stay a matrix?

One first would think so.
However, R here behaves as S.

>From ?as.numeric of S-plus 3.4:
S> VALUE:
S>        ....
S> 
S>        as.numeric returns a vector like x, but with storage  mode
S>        "double",  if  x  is  a  simple  object of mode "numeric".
S>        Otherwise, as.numeric returns a numeric object of the same
S>        length  as  x  and  with  data resulting from coercing the
S>        elements of x to mode "numeric".

The following shows the old (in S) quite usual ``workaround''.
The following code works `identically' in R and S :

> Mas.numeric <- function(x){ mode(x) <- "numeric"; x}
> M <- cbind("1",1:3); M
     [,1] [,2]
[1,] "1"  "1" 
[2,] "1"  "2" 
[3,] "1"  "3" 
> as.numeric(M)
[1] 1 1 1 1 2 3
> Mas.numeric(M)
     [,1] [,2]
[1,]    1    1
[2,]    1    2
[3,]    1    3
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-