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;