R-beta: Re: S Compatibility (again)

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Mon, 13 Apr 1998 00:06:17 +0930


Date: Mon, 13 Apr 1998 00:06:17 +0930
Message-Id: <9804121436.AA23034@attunga.stats.adelaide.edu.au>
From: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
To: Ross Ihaka <ihaka@stat.auckland.ac.nz>
Subject: R-beta: Re: S Compatibility (again)
In-Reply-To: <9804120750.AA22905@attunga.stats.adelaide.edu.au>

More for the record.  structure() does not coerce on one but does
on the other.

Consider changing a character matrix to numeric, keeping all
other attributes:

R:					  S:

> x <- matrix(paste(1:9), 3, 3)           > x <- matrix(paste(1:9), 3, 3)
> x                                       > x
     [,1] [,2] [,3]                            [,1] [,2] [,3]
[1,] "1"  "4"  "7"                        [1,] "1"  "4"  "7"
[2,] "2"  "5"  "8"                        [2,] "2"  "5"  "8"
[3,] "3"  "6"  "9"                        [3,] "3"  "6"  "9"

The obvious way fails on both systems, destroying dim attributes
(which may be a feature):

> as.numeric(x)                           > as.numeric(x)
[1] 1 2 3 4 5 6 7 8 9                     [1] 1 2 3 4 5 6 7 8 9

Using "mode<-" works on both:

> y <- x                                  > y <- x               
> mode(y) <- "numeric"			  > mode(y) <- "numeric" 
> y                    			  > y
     [,1] [,2] [,3]                             [,1] [,2] [,3]
[1,]    1    4    7                       [1,]    1    4    7
[2,]    2    5    8                       [2,]    2    5    8
[3,]    3    6    9                       [3,]    3    6    9

Using structure() fails on R (in an odd way) but works on S:

> structure(x, mode="numeric")            > structure(x, mode="numeric")
    [,1] [,2] [,3]                             [,1] [,2] [,3]
[1,] "1"  "4"  "7"                        [1,]    1    4    7
[2,] "2"  "5"  "8"                        [2,]    2    5    8
[3,] "3"  "6"  "9"                        [3,]    3    6    9
attr(,"mode")
[1] "numeric"

Finally note that assigning to the `open bracket' form does a
coercion on both.  (This is a standard way of hanging on to dim
and dimnames attributes in assignments, but it keeps "mode" as
well, so can't be used for this job.) 

> x[] <- 0:8                              > x[] <- 0:8
> x                                       > x
     [,1] [,2] [,3]                            [,1] [,2] [,3]
[1,] "0"  "3"  "6"                        [1,] "0"  "3"  "6"
[2,] "1"  "4"  "7"                        [2,] "1"  "4"  "7"
[3,] "2"  "5"  "8"                        [3,] "2"  "5"  "8"
>                                         >

Bill.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._