Get or set the class of an object.
The class() function returns or sets the class attribute of an R object. The class determines how generic functions (like print, summary, plot) behave for that object. R uses S3 and S4 class systems for object-oriented programming.
Common classes include "numeric", "character", "logical", "data.frame", "matrix", "list", "factor", and "function". Many packages define their own classes for specialized objects.
| Name | Description | Optional |
|---|---|---|
x |
An R object. | No |
class(42) # "numeric"
class("hello") # "character"
class(mtcars) # "data.frame"
class(lm(y ~ x)) # "lm"