Re: [Rd] as(object,"list") as(object,"matrix") differences?

From: John Chambers <>
Date: Wed 08 Sep 2004 - 01:47:24 EST

Yes, there are differences, and you should expect some.

That as(object, "matrix") preserves dimnames is hardly an accident, since dimnames are part of the definition of a matrix.

as(object, "list") works like all coercion to basic datatypes. The method uses the corresponding old-style as.<class> function. In addition, if coercion is strict, all attributes are removed. See ?as.

The effect of this is that names would be removed, unless you used as(object, "list", strict=FALSE). (The coerce in dispatching methods is NOT strict, so a method for class "list" would see the names.)

This is consistent behavior, but yes, the elimination of names may be surprising. It's possible that future versions might treat names differently from other attributes, but there would need to be a strong argument.

The workaround is to define a new class that trivially extends "list", and then define classes to extend that instead:

 setClass("listWithNames", "list")

Then if x is an object from a class defined with contains="listWithNames", rather than contains="list",   as(x, "listWithNames")
would preserve names.

The problem here is that the definition of class (or dataype) "list" was never very clear as to whether names were an attribute or part of the datatype. The green book and S-Plus take the view that lists are just vectors, and the current R implementation follows that interpretation.

(There is also in S-Plus a different class, "named", specifically for the applications that lists with names typically deal with. But the spirit of that class is NOT equivalent to lists with a names attribute.)

As for extending basic datatypes by S4 classes, that's generally OK, provided you understand the consequences (pages 314-316 of the green book have a brief discussion).

And as for "wrapper" classes, there are no immediate plans. The basic datatypes do have class definitions:

R> getClass("list")

No Slots, prototype of class "list"

Extends: "vector"

The main candidates for wrapper classes are "matrix" and "array", since these do not have a fixed set of attributes (they may or may not have "dimnames"). Class "ts" is already a formal class.

John Chambers

Wolski wrote:
> Hello!
> as(object,"list") and as(object,"matrix") behave quite differently if it comes to their attributes.
> I define two classes. One of them "contains" a "list" the other a "matrix"
> setClass("myclass"
> ,representation(info="character")
> ,contains="matrix"
> )
> setClass("mylist"
> ,representation(info="character")
> ,contains="list"
> )
> #init
> dd<-matrix(1:6,nrow=2)
> rownames(dd)<-c("a","b")
> tt<-new("myclass",dd)
> tmp<-vector("list",4)
> names(tmp)<-1:4
> ll <- new("mylist",tmp,info="foo")
> rownames(tt)
> [1] "a" "b"
> > rownames(as(tt,"matrix"))
> [1] "a" "b"
> > names(ll)
> [1] "1" "2" "3" "4"
> > names(as(ll,"list"))
> #but
> names(ll@.Data)
> The difference in behaviour to which i would like to point your attention is that as(object,"matrix") preserves the "dimnames" but at the same time as(object,"list") drops the "names" attribute.
> Is it recomended not to use "contains" with old style classes?
> Are there plans to provide standarized S4 versions/wrappers for list, matrix etc... classes?
> /E
> PS.
> R : Copyright 2004, The R Foundation for Statistical Computing
> Version 1.9.1 Patched (2004-08-30), ISBN 3-900051-00-3
> ______________________________________________
> mailing list

John M. Chambers        
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web:

______________________________________________ mailing list
Received on Wed Sep 08 01:53:48 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:00:06 EST