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:

    1. How do you tell what OO system (S3 vs. S4) an object is associated with?

      Using isS4(obj). If TRUE, It is S4.
      If is.object(obj) is TRUE but isS4 is FALSE, it is S3.

    2. 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 using typeof(x) or class(x)For detailed structure, including types of columns, use str(x). The is.type() family (e.g.is.integer()) checks for specific types.

    3. 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.

    4. 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 $. 

In your GitHub, create two examples of S3 and S4.
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

Popular posts from this blog

Assignment #9: Visualization in R – Base Graphics, Lattice, and ggplot2

Module # 6 Doing math in R part 2

Module #2 Assignment