← All languages
R function of the day
Random

class

Get or set the class of an object.

Description

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.

Arguments

NameDescriptionOptional
x An R object. No

Example

class(42)            # "numeric"
class("hello")       # "character"
class(mtcars)        # "data.frame"
class(lm(y ~ x))    # "lm"

Reference