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;