S3 class and Their Types
In R, everything is considered an object. Objects have attributes, and one of the most commonly associated attributes is the class. The class() command is used to define the class of an object or determine the class of an object.
Key Features of Class in R:
- Inheritance: Objects can inherit from multiple classes.
- Order of Inheritance: For complex classes, the order of inheritance can be specified.
Example: Checking the class of an object
# Creating a vector of fruits
fruits <- c("apple", "banana", "cherry")
# Checking the class of the vector
class(fruits)
Output:
[1] "character"
Example: Appending a class to an object
# Creating a vector of fruits
fruits <- c("apple", "banana", "cherry")
# Appending the class "Food" to the vector
class(fruits) <- append(class(fruits), "Food")
class(fruits)
Output:
[1] "character" "Food"
Managing Memory in R
When deciding between S3 and S4 classes in R, S4 offers a more structured approach, while S3 provides flexibility due to its less strict framework.
Memory environments contribute to S3’s flexibility. An environment is similar to a local scope, maintaining a variable set associated with it. Variables within the environment can be accessed if the environment’s ID is known.
To assign or retrieve variable values in an environment, assign() and get() commands are used.
Example: Assigning and retrieving variable values in an environment
$name
[1] "John"
$Roll_No
[1] 101
attr(,"class")
[1] "Student"
Output:
<environment: R_GlobalEnv>
[1] "colors" "env"
[1] "yellow" "purple" "orange"
Creating an S3 Class in R
S3 is the simplest and most widely used class system in R. An S3 object is essentially a list with assigned class attributes.
Steps to create an S3 object:
- Create a list containing the required components.
- Use
class()to assign a class name to the list.
Example 1: Creating an S3 object for employee details
# Creating a list for employee details
employee <- list(name = "Ravi", emp_id = 1001, salary = 50000)
# Assigning the class "employee" to the list
class(employee) <- "employee"
employee
Output:
$name
[1] "Ravi"
$emp_id
[1] 1001
$salary
[1] 50000
attr(,"class")
[1] "employee"
Example 2: Creating an S3 object for book details
# Creating a list for book details
book <- list(title = "Data Science Basics", author = "John Doe", pages = 250)
# Assigning the class "book" to the list
class(book) <- "book"
book
Output:
$title
[1] "Data Science Basics"
$author
[1] "John Doe"
$pages
[1] 250
attr(,"class")
[1] "book"
Generic Functions in R
R uses generic functions, which are capable of operating differently on various classes of objects. A common generic function is print().
For example, the print() function can print different types of objects like vectors, data frames, and matrices. This is possible because print() is a generic function comprising multiple methods.
Example: Methods of the print() function
# Checking methods available for the generic print() function
methods(print)
Output:
[1] print.data.frame print.default print.matrix
Leave a Reply