From ggrothendieck at myway.com  Fri Jul 30 04:14:33 2004
From: ggrothendieck at myway.com (Gabor Grothendieck)
Date: Fri Jul 30 04:14:43 2004
Subject: [Rd] Re: matrix subsetting  (was: [R] as(obj,
Message-ID: <20040730021433.E945F12CD7@mprdmxin.myway.com>



Suggest you try R 1.9.1 patched.  This is what I get on 
Windows XP with that:

> matrixObj <- array(1:4, c(2,2))
> class(matrixObj) <- "matrix"
> fooObj <- matrixObj
> class(fooObj) <- "foo"
> fooObj[1:2]
[1] 1 2
> matrixObj[1:2]
[1] 1 2
> getAnywhere("[.matrix")
no object named '[.matrix' was found
> getAnywhere("[.foo")
no object named '[.foo' was found
> 
> args("[.matrix")
Error in args("[.matrix") : couldn't find function "[.matrix"
> body("[.matrix")
Error in get(x, envir, mode, inherits) : variable "[.matrix" of mode "function" was not found
> R.version.string
[1] "R version 1.9.1, 2004-07-13"







Date:   	Thu, 29 Jul 2004 21:46:27 +0100
From:   	Patrick Burns <pburns@pburns.seanet.com>
To:   	John Chambers <jmc@research.bell-labs.com>, <r-devel@stat.math.ethz.ch>
Subject:   	[Rd] Re: 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:

>Wolski wrote:
>
>
>>Hi!
>>
>>Here a simple example.
>>
>>setClass("myclass"
>>,representation(info="character")
>>,contains="matrix"
>>)
>>
>>rownames(dd)<-c("a","b")
>>tt<-new("myclass",dd)
>>#the source of pain.
>>as(tt,"matrix")<-matrix(1,3,3)
>>Error: length of dimnames [1] not equal to array extent
>>
>>Is there a different way to do what I would like to do (I would like to change the @.Data and all its attributes in the object)?
>>
>>
>
>It's not particularly a problem with as(). Your class just doesn't
>behave as you expect.
>
>"matrix" is not a formal class with slots. (It isn't even an S3 class
>in R; attr(x,"class") is NULL.) So you cannot expect classes extending
>"matrix" to know what a matrix is supposed to be.
>
>Part of the problem is that matrix objects sometimes have dimnames and
>sometimes don't. And there is basic code in R that assumes or applies
>constraints on the "dim" or "dimnames".
>
>In S-Plus, "matrix" is a formal class, always having a slot for
>dimnames. R has not gone that route, at least not yet.
>
>It may be possible to define a new class, "Matrix", say, that looks like
>a matrix to old-style code but has a formal definition. But the details
>are likely to be tricky, and it's definitely a topic for r-devel, not
>r-help.
>
>John Chambers
>
>
>
>>Eryk.
>>


[Rd] Re: matrix subsetting (was: [R] as(obj,
Gabor Grothendieck