Skip to content
Prev 44837 / 398506 Next

Inheriting from factors + co.

On 25 Feb 2004 at 10:00, Uwe Ligges wrote:

            
Yes, and there is the problem. With S4 classes, it works. Now I would 
expect that is(factor(S3object, "ANY") will be FALSE for factor and any 
other S3 class, but is(S3object, "ANY") always gives TRUE.
It is of class "myclass" but it should inherit from "factor" by the .Data 
part if I specify a factor. That's what it does with S4 classes:

my1 <- new("myclass", 1:10)
class(my1)			#  "myclass"
is.numeric(my1)		# TRUE

my2 <- new("myclass", "abc")
class(my2)			#  "myclass"
is.character(my2)		# TRUE

In those cases the object actually is a numeric or a character and thus 
can be treated accordingly.
That works, of course. But that's not what I'm looking for. This requires 
using the slot explicitly (not impossible, but I'd really prefer to have it in 
the .Data part). One main drawback is that R just doesn't accept S3 
classes as .Data part:

setClass("myclass", representation("factor"))

It creates a new class (although a warning is issued) and also extends 
from "factor" but doesn't create the .Data slot. That's also the reason 
why my second approach with setClassUnion() doesn't work as soon as 
an S3 class is part of the union. I just don't understand why S3 classes 
in explicit slots work perfectly well whereas it seems impossible to have 
them in the .Data slot. 

Torsten