Module # 7 R Object: S3 vs. S4 assignment
Module # 7 assignment
VaShay Carpenter
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.
In your blog, discuss the following questions:
How do you tell what OO system (S3 vs. S4) an object is associated with?
UsingisS4(obj). IfTRUE, It is S4.
Ifis.object(obj)isTRUEbutisS4isFALSE, it is S3.How do you determine the base type (like integer or list) of an object?
You can determine the base type of an object in R usingtypeof(x)orclass(x)For detailed structure, including types of columns, use str(x). The is.type() family (e.g.is.integer()) checks for specific types.What is a generic function?
In R, a generic function is a function whose specific implementation (called a method) depends on the class of its arguments.What are the main differences between S3 and S4?
S3 is a flexible and informal object-oriented system in R suited for simple, quick tasks, while S4 is a formal, strict system designed for complex, large-scale projects requiring data integrity. Key differences include S4's mandatory class definitions, multiple dispatch (using multiple arguments), and using @ for slot access rather than $.
Github Link: https://github.com/cryo-cell/r-programming-assignments/blob/main/module7s3s4.Rmd
Example of data set for S3
s <- list(name = "Myself", age = 29, GPA = 3.5) > s3
$name
[1] "Myself"
$age
[1] 29
$GPA
[1] 3.5
For S4 it is a reminder, S4 objects are created using the new() function.
> s4 <- new("student",name="Myself", age=29, GPA=3.5)
> s4
An object of class "student"
Slot "name":
[1] "Myself"
Slot "age":
[1] 29
Slot "GPA": [1] 3.5
Disclaimer:
Generative AI is integrated into my professional workflow for drafting, structural organization, and code optimization. To avoid redundancy, this statement serves as a standing disclaimer for all entries. Generative AI has been utilized to ensure technical accuracy and to facilitate the very documentation requirements mandated by the curriculum available within the course syllabus.
Comments
Post a Comment