extends(class1, class2, maybe) is(object, class2)
extends
or
is
relation holds.
extends("matrix", "array")
. Default
TRUE
;
FALSE
and
NA
are reasonable alternatives.
is
returns
TRUE
or
FALSE
according to whether
object
has an
is
relation to
class2
,
i.e. whether
object
has (or inherits from)
class
class2
.
If
class2
is omitted, a character vector of the known
classes for which the relation holds is returned.
extends
tests whether
class1
extends
class2
and returns
TRUE
or
FALSE
correspondingly.
If the relation is conditional,
maybe
is returned (
TRUE
by default).
is
plays a narrow role, of testing whether an object
has a new-style class (
class(x)
, not
oldClass(x)
).
There are other functions which are useful
for testing for specific behavior.
For example,
tests whether an object is numerical,
test whether the object has double-precision data,
and
tests whether the object has two dimensions,
whether or not the object has the corresponding new-style class.
For example, a data frame has two dimensions, and may be subscripted
like a matrix, even though it does not have new-style class
"matrix"
.
A list of such functions can be obtained by doing:
methods("is")
.
# Define n as no. of rows or else length if(is(x, "matrix")) n = el(dim(x), 1) else n = length(x) # Similar, but with only the class to work from # we construct a function definition if(extends(class, "matrix")) nFunction = function(x) el(dim(x), 1) else nFunction = getFunction("length") x <- runif(6) names(x) <- letters[1:6] class(x) # "named" is(x, "double") # F -- because x has class "named", not "double" is.double(x) # T is(x, "numeric") # T -- because the "named" class conditionally inherits # from the virtual class "numeric", depending on the # type of the data. Here the condition is met. is(x) # List all classes a "named" object could inherit from. df <- data.frame(a=1:3, b=2:4) class(df) # "data.frame" is(df, "matrix") # F -- because df has class "data.frame", not "matrix" is.matrix(df) # T methods("is")