Defining a new S3 class
In OOP, the class and method play an important role. The behavior of a generic function depends on the class and the methods dispatched on it. In this recipe, you will create a new S3 class for an object in R.
Getting ready
The S3 class is the most popular class in R. Primarily, the majority of R objects come with default S3 classes. There is no rigid definition of S3 classes in R. Rather it is simple and intuitive to use. In this recipe, you will create a new S3 class, say robustSummary
, for a numeric vector. Let's say you have a numeric vector of the default class numeric
, but your objective is to create a new class robustSummary
and attach it to the object.
Here is the given numeric object with 50 items representing the number of hours spent on social media in a week:
x <- c(13, 21, 19, 18, 21, 16, 21, 24, 17, 18, 12, 18, 29, 17, 18, 11, 13, 20, 25, 18, 15, 19, 21, 21, 7, 12, 23, 31, 16, 19, 23, 15, 25, 19, 15, 25, 25, 16, 29, 15, 26, 29, 23,...