Module # 7 R Object: S3 vs. S4 assignment
Download any type of data (from the web or use datasets package) or create your own set.
Then, on the second step, determine if generic function as discussed in this module can be assigned to your data set, and if not, why? (Example, here is list of data set in R)
data("mtcars")
head (mtcars, 6)
list(mtcars, 6)
In third and last step, explore if S3 and S4 can be assigned to your data set.
First Step:
The dataset that I will be using is a built in dataset that is found in R. It is "airquality" New York Air Quality Measurements
Second Step:
Generic functions can be assigned to this dataset. You can see from the images below.
1) How do you tell what OO system (S3 vs. S4) an object is associated with?
By using class(), we are able to see the class of airquality.
2) How do you determine the base type (like integer or list) of an object?
To determine if the base type of an object you can use either typeof() or mode().
3) What is a generic function?
A generic function is used to determine the class of its arguments and select the appropriate method. A generic function is a function with a collection of methods. An example of a generic function would be summary(airquality).
4) What are the main differences between S3 and S4?
S3 objects, classes, and methods have been available in R from the beginning. They are informal and interactive. They are older, simpler, more dynamic, less structured, and has a single dispatch.
S4 objects, classes, and methods are more formal and structured with less interactive capabilities. They are newer, more structured, more rigorous, and have multi-dispatch
5) In your GitHub, create two examples of S3 and S4.
For this I wanted to create an example of S3 and S4 with airquality.
I then made my own examples of S3 and S4 with the given code of the example dataset.
Comments
Post a Comment