R Programming Journal Module 7 - Wesley Huang
- Wesley Huang
- Mar 2
- 2 min read
In this assignment, I created my own dataset in R representing student information, including names, ages, and GPAs. The dataset was stored as a data frame called students. This allowed me to analyze how generic functions operate on real data and determine which object-oriented system the dataset belongs to.
To determine the object-oriented system, I used functions such as class(), typeof(), and isS4(). The results showed that the dataset is a data frame, which is part of the S3 system. It is not associated with S4, as confirmed by the isS4() function returning FALSE.
I also applied generic functions such as summary() and head() to the dataset. A generic function is a function that behaves differently depending on the class of the object passed to it. For example, summary() produces descriptive statistics for numeric columns when applied to a data frame, but it would produce different output for other object types.
To determine the base type of the object, I used the typeof() function. The dataset is internally stored as a list, which is typical for data frames in R.
I then explored S3 and S4 systems using examples. S3 is informal and flexible, allowing objects to be created without strict class definitions. I created an S3 object using a list and assigned it a class. I also defined a custom print method for that class.
S4 is a more formal system that requires explicit class definitions. I created an S4 class called “student” with specific slots for name, age, and GPA. An S4 object was then created using the new() function.
The main differences between S3 and S4 are structure and strictness. S3 is simpler and more flexible but less formal, while S4 enforces strict definitions and type checking, making it more robust for complex applications.
Overall, this assignment helped me understand how R’s object-oriented systems work, how to identify an object’s type, and how generic functions adapt to different classes.
Below is a screenshot showing my input and output from R.




Comments