Which object identifier should I use?

Asked

Viewed 136 times

2

There are several functions that serve to identify objects in the R (or at least "think" they are 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.

Thus,

  • what are the differences between these functions?
  • in which contexts each of them applies?
  • why there is difference in return between different objects (x and reg)?

1 answer

2


class evaluates what is 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 the result of lm. Do methods(class = class(iris)) and you will see all the functions that can be applied in a data.frame

str is basically a form of show its object in a summarized form, so that you can have a quick View of all the content and structure of it, as well as its type. It just printa the content, that is, has no return value.

mode, storage.mode and typeof show how an object is stored internally in R. Whole data.frame is a list, so mode(iris) returns a list (as it is like an R stores internally) and not a data.frame (via class). The difference of numeric for mode(x) and integer for storage.mode(x) and typeof(x) can be found in ?mode (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 mode returns numeric for integer and double (unlike typeof).

Finally, mode and storage.mode use the typeof and return a different object; in addition to being able to assign a new object type. Mode is more compatible with the language S; typeof returns the "type" of the object from the R point of view (see here). This example of help(mode) 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 short:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.