For this week's assignment, I will start out by examining the iris dataset and then transition to my own dataset when it comes to creating two examples of S3 and S4.
Question 1: Determine if a generic function can be applied to your dataset
To begin, I used the following functions on the iris dataset and came up with the following output:
Based on this output, I can confirm that a generic function can be applied to my chosen dataset.
Question 2: How do you tell what OO system (S3 vs. S4) an object is associated with?
In the library, pryr, one can use the function otype() to determine which OO system object is associated with.
In this case, the iris dataset is associated with S3.
Question 3: How do you determine what the base type of an object is?
Using the typeof() function can help in determining an object's base type. Continuing with the iris dataset, we can check the object type of each of the variables within the dataset:
Question 4: What is an generic function?
A generic function can be defined as a function that performs a common task like printing (print()) or even plotting (plot()). Furthermore, they can be thought as extended function objects because they contain information that is used in creating and dispatching for the function.
Question 5: What are the main differences between S3 and S4?
To put it simply, S3 is considered more convenient while S4 is more safe. Additionally, S3 classes are very straightforward to implement as it only uses the first argument to dispatch but it can allow for mistakes to slip through like misspelled values and missing values and will not alert the programmer of the potential issues. On the other hand, S4 classes and methods are way more formal and more closely related to object-oriented concepts and unlike S3, S4 will complain about such misspellings and other issues to alert the programmer that the current code does need to be fixed.
Question 6: Create two examples of S3 and S4. (Code will be linked to GitHub)
S3 Code:
Output:
S4: Code:
Output:
After conducting this brief code experiment, I must admit that I greatly prefer the form of S4 over S3 just for its ease of creating instances of the class.
Link to GitHub Code: Module 7 Code
~ Katie
No comments:
Post a Comment