Learn R Programming

Matrix (version 0.3-19)

asObject: Assign or Modify the Class of an Object

Description

asObject assigns a class to an object and returns the object. prependClass prepends a class to the set of classes for an object and returns the object.

Usage

asObject(x, cl)
prependClass(x, cl)

Arguments

x
an object.
cl
a character vector of classes.

Value

  • the object x with the class cl (or c(cl, class(x)) for prependClass).

Details

asObject is simply a wrapper for the sequence class(x) <- cl; x. Its main use is in constructing the value that a function will return. It differs from the assignment class(x) <- cl in that the assignment returns the class whereas asObject returns the object with the class.

prependClass is a similar function that extends the classes (if any) of the object and returns the object.

Examples

Run this code
x <- asObject(3, "myClass")
print.default(x)
print.default(prependClass(x, "firstClass"))

Run the code above in your browser using DataLab