Which class identifier should I use?

2

There are several functions that can be used to identify classes in R (or at least "think" for this purpose), such as:

class()
mode()
typeof()
storage.mode()
str()

That, with a simple example:

x<-(1:5)

class(x) # [1] "integer"
mode(x) # [1] "numeric"
typeof(x) # [1] "integer"
storage.mode(x) # [1] "integer"
str(x) # int [1:5] 1 2 3 4 5

or

print(c(class(x),mode(x),storage.mode(x),typeof(x),str(x)))
#  int [1:5] 1 2 3 4 5
  # [1] "integer" "numeric" "integer" "integer"

do not return the same result. More precisely, mode is the only function that returns a different result.

Now, consider:

y<-c(1:100)
x<-c(101:200)

reg<-lm(y~x)
summary(reg)

class(reg) # [1] "lm"
mode(reg) # [1] "list"
typeof(reg) # [1] "list"
storage.mode(reg) # [1] "list"
str(reg) # List of 12
# $ coefficients : Named num [1:2] 1 1
 # ..- attr(*, "names")= chr [1:2] "(Intercept)" "a"

and, again, do not return the same result. And, this time, class is the only function that brings a different return.

So,

  • What are the differences between these functions?
  • In what contexts does each apply?
  • Why is there difference in return between different objects ( x and reg )?
asked by anonymous 20.10.2018 / 03:47

1 answer

2

class evaluates the class of the object. Objects in R have different functions that can be assigned to them depending on the class. You can use this function together with methods() to identify which functions can be applied to your object. For example, methods(class = class(reg)) :

 [1] add1           alias          anova          case.names     coerce         confint        cooks.distance deviance      
 [9] dfbeta         dfbetas        drop1          dummy.coef     effects        extractAIC     family         formula       
[17] hatvalues      influence      initialize     kappa          labels         logLik         model.frame    model.matrix  
[25] nobs           plot           predict        print          proj           qr             residuals      rstandard     
[33] rstudent       show           simulate       slotsFromS3    summary        variable.names vcov

displays all functions that are compatible with lm result. Make methods(class = class(iris)) and you will see all the functions that can be applied in a data.frame

str is basically a way to show your object in a summarized way, so that you can get a quick overview of all the content and its structure, as well as its type. It only contains% content, that is, it has no return value.

printa , mode , and storage.mode show how an object is stored internally in R. All typeof is a list, so data.frame returns a list (because it is how an R stores internally) and not a mode(iris) (via data.frame ). The difference of class to numeric and mode(x) to integer and storage.mode(x) can be found in typeof(x) (Mode names):

Modes have the same set of names as types (see typeof) except that

 types "integer" and "double" are returned as "numeric".

 types "special" and "builtin" are returned as "function".

 type "symbol" is called mode "name".

 type "language" is returned as "(" or "call".

Where the first line says that ?mode returns mode to numeric and integer (as opposed to double ).

Finally, typeof and mode use storage.mode and return a different object; in addition to being able to assign a new type of object. typeof is more compatible with Mode language; S returns the "type" of the object from the point of view of R (see here ). This example of typeof illustrates this well:

 cex3 <- c("NULL", "1", "1:1", "1i", "list(1)", "data.frame(x = 1)",
           "pairlist(pi)", "c", "lm", "formals(lm)[[1]]",  "formals(lm)[[2]]",
           "y ~ x","expression((1))[[1]]", "(y ~ x)[[1]]",
           "expression(x <- pi)[[1]][[1]]")
 lex3 <- sapply(cex3, function(x) eval(parse(text = x)))
 mex3 <- t(sapply(lex3,
                  function(x) c(typeof(x), storage.mode(x), mode(x))))
 dimnames(mex3) <- list(cex3, c("typeof(.)","storage.mode(.)","mode(.)"))
 mex3

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

In summary:

  • Use help(mode) to view your object quickly;
  • Use str(x) to check which functions can be applied to your object;
  • Use methods(class = class(x)) to see the type that your object is stored internally;
  • typeof(x) has very similar results of storage.mode , and typeof uses the results of mode and modifies them, in addition to both being able to modify the type of the object;
06.11.2018 / 12:50