Skip to content
Prev 13260 / 63421 Next

matrix subsetting (was: [R] as(obj,"matrix"))

On a related topic, a client came up with this example
a few days ago which I was unable to explain.  (But he
did heed my advise of "don't do that".)


 > matrixObj <- array(1:4, c(2,2))
 > class(matrixObj) <- "matrix"
 > fooObj <- matrixObj
 > class(fooObj) <- "foo"
 > fooObj[1:2]
Testing
     [,1] [,2]
[1,]    1    3
[2,]    2    4
attr(,"class")
[1] "foo"
 > matrixObj[1:2]
[1] 1 2
 > get("[.matrix")
function(x, i, j, drop = if(missing(i)) TRUE else length(cols)==1)
{   
    cat("Testing\n")
    x  
}
 > get("[.foo")
function(x, i, j, drop = if(missing(i)) TRUE else length(cols)==1)
{   
    cat("Testing\n")
    x  
}

Everything is the same except for the name of the class.  When
the class is "matrix", it ignores the subset method.  But the very
same code works if the class is "foo".  This is 1.9.1 Windows,
and the same happens with  1.7.0 (the oldest version I have on
my machine).

What is happening here?


Patrick Burns

Burns Statistics
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
John Chambers wrote: