Defining methods for the S3 class
A method is just an implementation of a generic function of any R object with a particular class of interest. The method in any OOP is the efficient way to perform certain customized tasks. Once you have a class and associated method, then you just need to call the generic function and the method. The method actually does not perform any operation, rather it just dispatches a specific function based on the class of the objects. For example, when you call print()
on a data.frame
object, it calls print.data.frame()
. In this recipe, you will define a new method for the newly defined class robostSummary
so that it only shows robust descriptive statistics of an object.
Getting ready
In R, generally if you write the object name into the console, then it automatically prints its internal elements. For example, take a look at the following code snippet:
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,...