Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically distinguishing S3 and S4 objects in R

Tags:

object

r

s4

If given an object x, is there a way to classify whether or not it is S3 or S4 (or "other")? I have looked at is.object() and isS4(), and can identify that something is an object (or not) and that it is an S4 object (or not). However, it doesn't seem to me that S3 objects are the complement of all objects that are not S4 objects.

Therefore, how can these assignments be done programmatically?

Here is an example of something that bugs me, taken from the help for is.object():

a = as.factor(1:3)
is.object(a)  # TRUE
isS4(a) # FALSE

Does that mean that a is an S3 object?

like image 744
Iterator Avatar asked Aug 05 '11 16:08

Iterator


1 Answers

If it is an object and is not an S4 then it is an S3:

is.object(foo) & !isS4(foo)

is.object checks for some magic OBJECT bit that gets set when the thing has a class attribute, so its essentially a fast way of doing any(names(attributes(foo))=="class"), which is what defines an S3 object.

like image 175
Spacedman Avatar answered Oct 23 '22 04:10

Spacedman