Creating a function to return an object of the S4 class
In R, whenever you write a function, by default the return object gets an S3 class but if you want to return an object that has S4 class, then you should explicitly define the class within the body of the function. In the previous recipes, you saw that the class has been defined outside of the function body. In this recipe, you will write a function that will return an object of the S4 class.
Getting ready
Your objective is to create a function that will calculate the following two types of descriptive statistics:
- Classical (mean and standard deviation)
- Robust (median and MAD)
The following is the vector of numeric values for the input. Your new function will take two inputs, one is the numeric vector and another is the type (either "classical"
or "robust"
). The function will return an object of the S4 class containing the following results:
x <- c(13, 21, 19, 18, 21, 16, 21, 24, 17, 18, 12, 18, 29, 17, 18, 11, 13, 20, 25, 18...