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

John Chambers jmc at research.bell-labs.com
Wed Sep 8 16:10:51 CEST 2004


Please re-read the previous mail to see answers to your questions, as
noted below.

Wolski wrote:
> 
> Hi!
> 
> Some follow up questions concerning the class definition of list.
> 
> On 9/7/2004 at 11:47 AM John Chambers wrote:
> >>>
> >>>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.

NOTE -------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> >>>
> >>>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.)
> >>>
> setClass("Mlist"
>          ,representation(info="character") #uniq names have to be unique?
>          ,contains="list"
>          ,prototype(uniq=FALSE)
>          )
> 
> tmp <- as.list(1:10)
> names(tmp) <- letters[1:10]
> mylist<-new("Mlist" , tmp , content="numeric" ,uniq=FALSE)
> 
> as.list(tmp) #preserves names
> as.list(tmp,strict=TRUE) #preserves names

You are misusing as.list, it has no strict=argument.  See ?as.list.

> 
> In both cases R preserves the names attribute! So it seems to me that current R's list definition includes the names attribute.
> 
> But
> 
> as(mylist,"list") #names are gone.
> 
> does not.

As previously explained.

> 
> >>>datatype.  The green book and S-Plus take the view that lists are just
> >>>vectors, and the current R implementation follows that interpretation.
> 
> are "as.list" as it is, is deprecated?

No, but it belongs to a different, earlier programming model.  The
current implementation maps the "basic" classes such as list in a
particular way, but it's not possible to be entirely compatible with
earlier functions which did not always follow a consistent class model
(naturally, since there wasn't one at the time).  We can discuss whether
to change the implementation, but there needs to be a good general
argument for doing so.

> 
> Eryk
> 
> *********** REPLY SEPARATOR  ***********
> 
> >>>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"))
> >>>> NULL
> >>>> #but
> >>>> 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?
> >>>>
> >>>> /E
> >>>>
> >>>> PS.
> >>>> 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
> >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>>
> >>>--
> >>>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
> 
> Dipl. bio-chem. Witold Eryk Wolski             @         MPI-Moleculare Genetic
> Ihnestrasse 63-73 14195 Berlin                'v'
> tel: 0049-30-83875219                        /   \
> mail: witek96 at users.sourceforge.net        ---W-W----    http://www.molgen.mpg.de/~wolski
>       wolski at molgen.mpg.de

-- 
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 mailing list