class
Class Attribute of an Object
Description
Return or change the class attribute of an object.
Usage
class(x)
class(x) <- value
unclass(x)
.class2(x)
Arguments
x |
an object. If you need to set the class to factor, x must be an integer object.
|
value |
a string vector listing the class names, or NULL.
|
Details
Every object in the engine has a class. Use class to set the class of new objects or
modify the class of existing objects. Using the assignment operator (that is, class(x) <- value)
sets the class of the object to that of the class specified by value.
This assignment technique is useful for functions that create
objects by building on existing objects of a particular class. In these cases, the object specified by
x should be a prototype for the intended class.
The class of an object determines the methods from a generic function that are available in a given instance.
To work with an object in raw form, without any assistance from methods available to process
that class, you can remove the object class by using the unclass function.
Assigning a class to an object is a primitive operation. This means that, if you assign a class to an object,
that object is not coerced to have all the attributes of an object of that class.
To coerce an object to a different, specific class, you must use the as function.
For example, to coerce an object x to "matrix", you must use the
as.matrix(x) function. Similarly, class is not a generic function;
therefore you cannot write methods for it.
Value
class | returns a character string containing the class of the object specified in x. |
unclass | returns the representation of x as an object with the class of the prototype
for class(x). If x has a representation with slots, unclass(x)
returns a named list, or structure, containing the slots in that representation. |
.class2 | returns a character string vector giving the
class names used for S3 method dispatching of the object x. |
See Also
Examples
x <- data.frame(a=11:13, b=21:23)
class(x) # [1] "data.frame"
# declare a print method for the class foo
print.foo <- function(x, ...) cat("FOO:", NROW(x), "x", NCOL(x), "\n")
class(x) <- c("foo", class(x)) # add the foo class to x
x # print using the foo method
# FOO: 3 x 2
y <- unclass(x) # remove all class information
y # print like a list
.class2(matrix(1:4,2))
# [1] "matrix" "array" "integer" "numeric"