Generic Functions in R
What I Did
This assignment helped me learn how R's generic functions work and the difference between S3 and S4 objects. I used the mtcars dataset to see how functions change behavior based on what kind of object they're working with.
Loading the Dataset
First, I loaded mtcars
data("mtcars") head(mtcars) str(mtcars)
This dataset has 11 columns with things like miles per gallon, horsepower, Etc.
How Generic Functions Work
I tested some basic R functions to see how they adapt to different objects:
summary(mtcars) plot(mtcars)
lm_model = lm(mpg ~ wt + hp, data = mtcars) summary(lm_model)
What I noticed: when you run summary() on a data frame, you get min/max/mean values. But when you run it on a linear model, you get totally different outputs, like coefficients and p-values. Same function name, different behavior!
Creating S3 Objects
S3 is the simpler object system. Here's how I made one:
s3_obj = list(name = "Omar", age = 22, GPA = 3.8) class(s3_obj) = "student_s3"
print.student_s3 = function(x) { cat("S3 Student:", xname,"∣Age:",xname,"∣Age:",xage, "| GPA:", x$GPA, "\n") }
Creating S4 Objects
S4 is more formal and requires defining the structure first:
setClass("student_s4", slots = c(name = "character", age = "numeric", GPA = "numeric"))
s4_obj = new("student_s4", name = "Omar", age = 22, GPA = 3.8)
I had to be more specific about data types with S4.
Key Takeaways
How do I check if something is S3 or S4? I used isS4() - it returns TRUE for S4 objects and FALSE for S3.
How do you see the base type? The typeof() function shows what's underneath, like "list" or "double".
What's a generic function? It's a function that acts differently depending on what you give it. Like how plot() makes different graphs for different data types.
S3 vs S4 - what's the difference? S3 is casual and flexible. I don't need much setup. S4 is stricter
I define everything upfront, and it checks my data types. S3 uses $ to access stuff, S4 uses @.
What I Learned
Generic functions are pretty smart and simple. they figure out what method to use based on the object class. When there's no specific method (like calling mean() on my student object), R just returns NA or uses a default.
What I Did
This assignment helped me learn how R's generic functions work and the difference between S3 and S4 objects. I used the mtcars dataset to see how functions change behavior based on what kind of object they're working with.
Loading the Dataset
First, I loaded mtcars
data("mtcars") head(mtcars) str(mtcars)
This dataset has 11 columns with things like miles per gallon, horsepower, Etc.
How Generic Functions Work
I tested some basic R functions to see how they adapt to different objects:
summary(mtcars) plot(mtcars)
lm_model = lm(mpg ~ wt + hp, data = mtcars) summary(lm_model)
What I noticed: when you run summary() on a data frame, you get min/max/mean values. But when you run it on a linear model, you get totally different outputs, like coefficients and p-values. Same function name, different behavior!
Creating S3 Objects
S3 is the simpler object system. Here's how I made one:
s3_obj = list(name = "Omar", age = 22, GPA = 3.8) class(s3_obj) = "student_s3"
print.student_s3 = function(x) { cat("S3 Student:", xname,"∣Age:",xname,"∣Age:",xage, "| GPA:", x$GPA, "\n") }
Creating S4 Objects
S4 is more formal and requires defining the structure first:
setClass("student_s4", slots = c(name = "character", age = "numeric", GPA = "numeric"))
s4_obj = new("student_s4", name = "Omar", age = 22, GPA = 3.8)
I had to be more specific about data types with S4.
Key Takeaways
How do I check if something is S3 or S4? I used isS4() - it returns TRUE for S4 objects and FALSE for S3.
How do you see the base type? The typeof() function shows what's underneath, like "list" or "double".
What's a generic function? It's a function that acts differently depending on what you give it. Like how plot() makes different graphs for different data types.
S3 vs S4 - what's the difference? S3 is casual and flexible. I don't need much setup. S4 is stricter
I define everything upfront, and it checks my data types. S3 uses $ to access stuff, S4 uses @.
What I Learned
Generic functions are pretty smart and simple. they figure out what method to use based on the object class. When there's no specific method (like calling mean() on my student object), R just returns NA or uses a default.
Comments
Post a Comment