[R] matrix no longer "is" array in 2.0.1?

Giles Heywood giles.heywood at cantab.net
Wed Jan 5 19:29:41 CET 2005


I have an S4 class which extends array and has other slots, and in upgrading
to 2.0.1 (from 1.9.1) I have encountered a change in behaviour. This causes
me some difficulties if I want to allow 2-dimensional arrays in the slot
(which I do).

The following (in 2.0.1) illustrates the point:

> setClass("foo",representation("array"))
[1] "foo"
> a <- new("foo",array(NA,2:4))
> b <- new("foo",matrix(NA,2,3))
Error in "as<-"(`*tmp*`, Classi, value = c(NA, NA, NA, NA, NA, NA)) :
        No method or default for as() replacement of "foo" with
Class="matrix"

This last error did not occur under 1.9.1.

This becomes a practical consideration in an extract method:

> setMethod("[", c("foo"),  function(x, i, j, ..., drop)
+     {
+     if(missing(i)) {i <- min(1,nrow(x)):nrow(x)}
+     if(missing(j)) {j <- min(1,ncol(x)):ncol(x)}
+     if(missing(drop)) {drop <- TRUE}
+     subx <- x at .Data[i, j, ..., drop = drop]
+     new("foo",subx)
+     })
[1] "["
> a[,,1]
Error in "as<-"(`*tmp*`, Classi, value = c(NA, NA, NA, NA, NA, NA)) :
        No method or default for as() replacement of "foo" with
Class="matrix"

This is changed behaviour from 1.9.1, for the reason above.

I can try to assign the matrix an array class, but the 2-dimensional object
has a special status, viz:

> class(as(1,"array"))
[1] "array"
> dim(as(1,"array"))
[1] 1

but:

> class(as(matrix(1,1,1),"array"))
[1] "matrix"

I have checked using extends(), and matrix and array still extend one
another (hence there is a trivial but unhelpful answer to the title of this
posting).

getClass() shows some differences, but I am not sure whether they are
related to this issue:

1.9.1

>getClass("matrix")

No Slots, prototype of class "matrix"

Extends: "structure", "array"

Known Subclasses:
Class "array", directly, with explicit test and coerce

2.0.1

>getClass("matrix")

No Slots, prototype of class "matrix"

Extends:
Class "structure", directly
Class "array", directly
Class "vector", by class "structure", with explicit coerce
Class "vector", by class "array", with explicit coerce

Known Subclasses:
Class "array", directly, with explicit test and coerce

My question is: how can I get a 2-dimensional object into the slot in my S4
class, or into the class "foo" in the minimal example above?

[OS = Windows XP]




More information about the R-help mailing list