[R] as(obj,"matrix")

John Chambers
Wed, 28 Jul 2004 15:42:43 -0000

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.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

--
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc