From Martin Maechler <maechler@stat.math.ethz.ch>  Wed Apr  9 07:34:29 1997
From: Martin Maechler <maechler@stat.math.ethz.ch> (Martin Maechler)
Date: Wed, 9 Apr 97 08:34:29 +0200
Subject: R-alpha: as.numeric
In-Reply-To: <Pine.SUN.3.91.970408100342.9023B-100000@chekov> (message from
 Thomas Lumley on Tue, 8 Apr 1997 10:04:19 -0700 (PDT))
Message-ID: <9704090634.AA01856@>


>>>>> "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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


R-alpha: as.numeric
Martin Maechler