§2023-08-15
In the R programming language, both typeof() and class() are used to determine the type of an object, but they serve slightly different purposes:
¶typeof(): This function returns a string that indicates the basic type of an object. It categorizes objects into one of the following types:
- "character": Character vector.
- "numeric": Numeric (floating-point) vector.
- "integer": Integer vector.
- "complex": Complex vector.
- "logical": Logical (boolean) vector.
- "list": List object.
- "function": Function object.
- "NULL": Null object.
- "expression": Expression object.
- "symbol": Symbol object.
- "pairlist": Pairlist object.
- "externalptr": External pointer object.
- "bytecode": Bytecode object.
- "environment": Environment object.
- "s4": S4 object.
- For example, typeof(5) would return "double" because it's a numeric vector with floating-point values.
¶class()
This function returns a character vector of class names that define the object's class and its inheritance hierarchy in the form of a class attribute. It is used primarily in the context of object-oriented programming in R. When you create your own classes (using S3 or S4 classes), the class() function is used to determine the class of an object and to retrieve all the classes in its inheritance hierarchy.
For example, if you create a class called "Person" and then create an object of that class, you can use class(person_object) to get the class name "Person".