Is.object and S3 class system

Using the class function allows us to define the class of the object:

 > x = 5 > class(x) [1] "numeric" 

I also understand that we can use the is.object command to determine if an object has a class. However, some types of objects are implicit, i.e.

 > is.object(x) [1] FALSE 

Would it be correct to say that all variables in R are objects, and is.object is a test only for implicit classes?

Just like types fit into that. Naively, I thought the following code fragment would lead to an error:

 > x = 5 > class(x) = "fake" > x = X + 1 > x + 1 [1] 6 attr(,"class") [1] "fake" 

But x is still of type double, but it still works. Can types be considered a superclass that all other objects inherit?

+4
source share
2 answers

typeof returns the type of the internal representation of C and is not used to send the method. So strictly speaking, you cannot think of types as "superclasses."

Instead of base classes (numerical, symbolic, list, function, etc.) that approximately correspond to the names, typeof returned, but not always (for example, the double type has a numerical class, the special function and the class closure, and the data.frame class has a list of types !).

With S3 and S4 systems, you can create non-trivial classes using base classes (but not necessarily one of them !!) Example: setClass("foo", list(a="numeric",b="character") not extends to none of the base classes ).

For objects from these base classes, is.object returns FALSE . As its documentation says, this function provides a very quick way to check if an object has a custom class S3 or S4 (i.e. not one of the base classes).

After casting x as "fake", your object does not formally belong to the "numerical" class:

 is(x, "numeric") #FALSE 

but it is interpreted as a basic "numerical" object:

 is.numeric(x) #TRUE 

And that's why + works here. So internally, since @Richie already said that the default method interprets x as a numeric base class.

This conceptual mess is due to an informal relationship with the S3 classes. Use S4 instead.


correspondence between typeof (.) and base class (.):

  typeof(.) class(.) NULL "NULL" "NULL" 1 "double" "numeric" 1:1 "integer" "integer" 1i "complex" "complex" list(1) "list" "list" data.frame(x=1) "list" "data.frame" pairlist(pi) "pairlist" "pairlist" c "special" "function" lm "closure" "function" formals(lm)[[1]] "symbol" "name" formals(lm)[[2]] "symbol" "name" y~x "language" "formula" expression((1))[[1]] "language" "(" (y~x)[[1]] "symbol" "name" expression(x <- pi)[[1]][[1]] "symbol" "name" 
+2
source

A partial answer to the first question is contained in chapter 2 of the definition of the language R

R does not provide direct access to computer memory, but provides a number of specialized data structures, which we will call objects. These objects are referenced through characters or variables. In R, however, the symbols themselves are objects, and they can be manipulated in the same way as any other object.

So yes, all variables are objects.

is.object seems more or less equivalent to function(x) !is.null(attr(x, "class")) , but I'm ready to make a mistake about it.

Regarding the second question, I think this is what happens: Since x has the class “fake”, R looks for the +.fake method in the append, but when it does not find it, it accesses the default method. This default method is based on C base code, which uses typeof(x) (or the C equivalent) to determine what needs to be done. In this case, type x is an "integer".

+2
source

Source: https://habr.com/ru/post/1335999/


All Articles