[Rd] as(object,"list") as(object,"matrix") differences?
jmc at research.bell-labs.com
Tue Sep 7 17:47:24 CEST 2004
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
The workaround is to define a new class that trivially extends "list",
and then define classes to extend that instead:
Then if x is an object from a class defined with
contains="listWithNames", rather than contains="list",
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:
No Slots, prototype of class "list"
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.
> 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"
> ll <- new("mylist",tmp,info="foo")
>  "a" "b"
> > rownames(as(tt,"matrix"))
>  "a" "b"
> > names(ll)
>  "1" "2" "3" "4"
> > names(as(ll,"list"))
> names(ll at .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?
> R : Copyright 2004, The R Foundation for Statistical Computing
> Version 1.9.1 Patched (2004-08-30), ISBN 3-900051-00-3
> R-devel at stat.math.ethz.ch mailing list
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
More information about the R-devel