[R] unclass

Martin Maechler maechler at stat.math.ethz.ch
Sat Aug 16 17:23:17 CEST 2003


 [BCC: to JMC]
>>>>> "kjetil" == kjetil brinchmann halvorsen <kjetil at entelnet.bo>
>>>>>     on Sat, 16 Aug 2003 10:39:25 -0400 writes:

    kjetil> Have I been sleeping in class?  

yeah, probably the one on S4 classes/methods and the NEWS that
the methods package is now always attached by default      ;-) :-0)
(yes, I'm joking).

    kjetil>    ...

    >> class(incidencia)
    kjetil> [1] "array"
    >> incidencia <- unclass(incidencia); class(incidencia)
    kjetil> [1] "array"

More specific: When the "methods" package is attached, every
object has a class, and I'd expect unclass() to be no-op.

HOWEVER, that's not quite true:

1) unclass() remains a no-op for "array" objects, even after detaching "methods".
   In R 1.6.1, we had

     > class(unclass(array(1:6, 1:3)))
     [1] "array"
     > detach("package:methods")
     > class(unclass(array(1:6, 1:3)))
     NULL
     > 

   This is different in R 1.7.1  (the class remains "array"),
   and this is even true when "methods" was never attached
   (i.e. when starting R with no default packages).

  I'll let other R-corers explain why this might be desired.
  We might consider  unclass(x) produce a warning whenever `x'
  has a class and that is not changed.

2) unclass() is not no-op for e.g. data frames; it returns a
   list, in both cases {"methods" attached, or not, the
   difference being that "list" is a class in the first case,
   but not the second}.  
  
  Note that this is very necessary: If  unclass() had become a
  no-op as soon as "methods" was attached, a lot of R code would
  break, notably also the case of  unclass(<factor>) !

--

So, thank you, Kjetil, this was an interesting question and I've
learned, too!

Martin




More information about the R-help mailing list