R Classes and Objects (With Examples) (2024)

We can do object oriented programming in R. In fact, everything in R is an object.

An object is a data structure having some attributes and methods which act on its attributes.

Class is a blueprint for the object. We can think of class like a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house.

House is the object. As many houses can be made from a description, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation.

While most programming languages have a single class system, R has three class systems. Namely, S3, S4 and more recently Reference class systems.

They have their own features and peculiarities and choosing one over the other is a matter of preference. Below, we give a brief introduction to them.

S3 Class

S3 class is somewhat primitive in nature. It lacks a formal definition and objects of this class can be created simply by adding a class attribute to it.

This simplicity accounts for the fact that it is widely used in the R programming language. In fact most of the R built-in classes are of this type.

See R programming S3 Class section for further details.

Example 1: S3 Class

s <- list(name = "John", age = 21, GPA = 3.5)class(s) <- "student"# print the list with the updated classprint(s)

Output

$name[1] "John"$age[1] 21$GPA[1] 3.5attr(,"class")[1] "student"

Here, the class attribute allows objects to be associated with a specific class. The class attribute provides a way to define behavior and methods for objects of that class.

By assigning the class attribute to the list s, we are essentially indicating that s is an object of the "student" class within the S3 object-oriented system.

S4 Class

S4 class is an improvement over the S3 class. They have a formally defined structure which helps in making objects of the same class look more or less similar.

Class components are properly defined using the setClass() function and objects are created using the new() function.

See R programming S4 Class section for further details.

Example 2: S4 class

setClass("student", slots = list(name = "character", age = "numeric", GPA = "numeric"))# create an instance of the "student" classs <- new("student", name = "John", age = 21, GPA = 3.5)# print the instanceprint(s)

Output

An object of class "student"Slot "name":[1] "John"Slot "age":[1] 21Slot "GPA":[1] 3.5

Here, the setClass() function is used to define an S4 class called "student" with three slots: name of type "character", age of type "numeric", and GPA of type "numeric".

After defining the class, we have created an instance of the "student" class using the new() function.

The new() function takes the class name and initializes the object by providing values for each slot.

Finally, the instance s is printed, displaying the object's class and the values stored in each slot.

Reference Class

Reference classes were introduced later, compared to the other two. It is more similar to the object oriented programming we are used to seeing in other major programming languages.

Reference classes are basically S4 classed with an environment added to it.

See R programming Reference Class section for further details.

Example 3: Reference class

# define the reference class "student"student <- setRefClass("student", fields = list( name = "character", age = "numeric", GPA = "numeric" ))# create an instance of the "student" reference classs <- student$new( name = "John", age = 21, GPA = 3.5)# print the instanceprint(s)

Output

Reference class object of class "student"Field "name":[1] "John"Field "age":[1] 21Field "GPA":[1] 3.5

Here, we have defined the reference class "student" using the setRefClass() function, specifying the class name and the fields within the class.

The fields argument is a list that defines the names and types of the fields in the class.

After defining the class, we have created an instance of the "student" class using the $new() method.

The $new() method takes arguments for each field and initializes the object with the provided values.

Finally, the instance s is printed, displaying the object's class and the values stored in each field.

Comparison between S3 vs S4 vs Reference Class

S3 ClassS4 ClassReference Class
Lacks formal definitionClass defined using setClass()Class defined using setRefClass()
Objects are created by setting the class attributeObjects are created using new()Objects are created using generator functions
Attributes are accessed using $Attributes are accessed using @Attributes are accessed using $
Methods belong to generic functionMethods belong to generic functionMethods belong to the class
Follows copy-on-modify semanticsFollows copy-on-modify semanticsDoes not follow copy-on-modify semantics
R Classes and Objects (With Examples) (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Laurine Ryan

Last Updated:

Views: 6470

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.