R-beta: Re: S Compatibility (again)
Bill Venables
wvenable at attunga.stats.adelaide.edu.au
Sun Apr 12 16:36:17 CEST 1998
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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list