Module #7 Assignment

 For this assignment, I used the popular mtcars dataset for my analysis. My code is as follows:

data("mtcars")

head(mtcars)

list(mtcars)


#we can create generic functions for mtcars such as functions to find the 

#mean or a function to create plots such as bar or scatter plots.


#S3 method

class(mtcars)


summary.mtcars <- function(object, ...) {

  summary(object)

}

summary(mtcars)

#S3 methods can be applied


#s4 method

setClass("mtcarsClass", contains = "data.frame")

mtcars_s4 <- new("mtcarsClass", mtcars)

mtcars_s4

#S4 methods can be applied




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

You can use the functions isS3method() and is4()
  1. How do you determine the base type (like integer or list) of an object?

You can use the class function
  1. What is a generic function?

A function that behaves according to the class it is applied to.
  1. What are the main differences between S3 and S4?

S4 is more complex and structured than S3. S3 does not have native support for slots unlike S4. S3 also uses a much simpler method dispatch. 
  1. In your GitHub, create two examples of S3 and S4.

Comments

Popular posts from this blog

Module #11 Assignment

Module #9