revolve around the real-life entities. Everything in R is an object. An object is
simply a data structure that has some methods and attributes. A class is just a
blueprint or a sketch of these objects. It represents the set of properties or
methods that are common to all objects of one type.
Unlike most other programming languages, R has a three-class system. These
are S3, S4, and Reference Classes.
S3 Class
S3 is the simplest yet the most popular OOP system and it lacks formal
definition and structure. An object of this type can be created by just adding an
attribute to it. Following is an example to make things more clear:
Example:
# create a list with required components
movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")
# give a name to your class
class(movieList) <- "movie"
movieList
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"
In S3 systems, methods don’t belong to the class. They belong to generic
functions. It means that we can’t create our own methods here, as we do in
other programming languages like C++ or Java. But we can define what a
generic method (for example print) does when applied to our objects.