Module 7 R Object: S3 vs. S4 Assignment
I used the built-in dataset mtcars to answer all the questions. I used the head() function to display the first six rows, so we see the structure of the data. This will help us understand what type of object we are working with before applying the functions. The summary() function is a generic function that will detect that mtcars is a data frame. The output that it gives us will provide a descriptive statistic (min, max, mean, quartiles) for each variable.
I had an error about the "figure margins too large" and that is because the plot window was too small. I had to use the par() function to reset the plot margins to default size. The plot() function creates a graph of miles per gallon values. The graph shows the results after using the plot() function. I used the class(mtcars) to make sure the object is "data.frame" and the isS4() function to check whether the object is S4. Since the output returned FALSE, it confirms that mtcars uses the S3 system. Next, I use the typeof() function to show the international storage type. The result from it shows "list" since the data frame is internally stored as a list of equal-length vectors. I used the str displays the structure and variable types.
1. How do you tell what OO system (S3 vs. S4) an object is associated with?
You can determine whether an object uses S3 or S4 by checking its class and testing if it is an S4 object. The class(object) function will show the object class. The isS4(object) will return TRUE if the object uses S4. If it returns FALSE, the object is most likely S3.
2. How do you determine the base type (like integer or list) of an object?
The base type refers to how the object is stored internally in memory. The function typeof(object) shows internal storage type, mode(object) shows storage mode, and str(object) shows the structure and element types.
3. What is a generic function?
A generic function is a function that performs different actions depending on the class of the object provided. Instead of one fixed behavior, the functions call a method that matches the object type. For example, the summary(), plot(), and print() function.
4. What are the main difference between S3 and S4?
S3 System is informal, flexible, widely used in base R, and methods are created using naming conventions. S4 Systems a formal, strict structure, more robust and reliable for complex systems,
5. In your GitHub, create two examples of S3 and S4?



Comments
Post a Comment