Module # 7 R Object: S3 vs. S4 assignment
Objects in R: S3 vs. S4
1) This week we have three steps to complete, as we learn about object oriented programing or OOP. First we need to find a data set. Then see if a generic function can be assigned to said data set. The last step is to see if S3 or S4 objects can be assigned to the data set. Here we go.
2) The data set I have chosen covers electric vehicle population. I chose this one because it was the easiest to obtain besides mtcars. You can find it here for free.
3) To see if a generic function can be assigned to this data set. To do this we need to first understand what a generic function is. A generic function in R, are functions designed to change their behavior based on the class of the input object. Some examples of this are "plot", "mean", "predict", "summary" etc. We are going to try to use "summary" on our data and see if we get back a summary of each column, custom made to that column's data type. Here we go:
> #STEP1
> #Get a data set from the web: "electric vehicle population data"
> myDataSet <- read.csv("CENSORED/Electric_Vehicle_Population_Data.csv")
> head(myDataSet)
VIN..1.10. County City State Postal.Code Model.Year Make Model Electric.Vehicle.Type
1 JTMAB3FV3P Kitsap Seabeck WA 98380 2023 TOYOTA RAV4 PRIME Plug-in Hybrid Electric Vehicle (PHEV)
2 1N4AZ1CP6J Kitsap Bremerton WA 98312 2018 NISSAN LEAF Battery Electric Vehicle (BEV)
3 5YJ3E1EA4L King Seattle WA 98101 2020 TESLA MODEL 3 Battery Electric Vehicle (BEV)
4 1N4AZ0CP8E King Seattle WA 98125 2014 NISSAN LEAF Battery Electric Vehicle (BEV)
5 1G1FX6S00H Thurston Yelm WA 98597 2017 CHEVROLET BOLT EV Battery Electric Vehicle (BEV)
6 5YJYGDEE5L Snohomish Lynnwood WA 98036 2020 TESLA MODEL Y Battery Electric Vehicle (BEV)
Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility Electric.Range Base.MSRP Legislative.District DOL.Vehicle.ID
1 Clean Alternative Fuel Vehicle Eligible 42 0 35 240684006
2 Clean Alternative Fuel Vehicle Eligible 151 0 35 474183811
3 Clean Alternative Fuel Vehicle Eligible 266 0 43 113120017
4 Clean Alternative Fuel Vehicle Eligible 84 0 46 108188713
5 Clean Alternative Fuel Vehicle Eligible 238 0 20 176448940
6 Clean Alternative Fuel Vehicle Eligible 291 0 21 124511187
Vehicle.Location Electric.Utility X2020.Census.Tract
1 POINT (-122.8728334 47.5798304) PUGET SOUND ENERGY INC 53035091301
2 POINT (-122.6961203 47.5759584) PUGET SOUND ENERGY INC 53035080700
3 POINT (-122.3340795 47.6099315) CITY OF SEATTLE - (WA)|CITY OF TACOMA - (WA) 53033007302
4 POINT (-122.304356 47.715668) CITY OF SEATTLE - (WA)|CITY OF TACOMA - (WA) 53033000700
5 POINT (-122.5715761 46.9095798) PUGET SOUND ENERGY INC 53067012510
6 POINT (-122.287143 47.812199) PUGET SOUND ENERGY INC 53061051922
>
> #STEP 2
> #See if generic function can be applied on the data set
> #EXPLAIN
> summary(myDataSet)
VIN..1.10. County City State Postal.Code Model.Year
Length:205439 Length:205439 Length:205439 Length:205439 Min. : 1731 Min. :1997
Class :character Class :character Class :character Class :character 1st Qu.:98052 1st Qu.:2019
Mode :character Mode :character Mode :character Mode :character Median :98125 Median :2022
Mean :98178 Mean :2021
3rd Qu.:98372 3rd Qu.:2023
Max. :99577 Max. :2025
NA's :3
Make Model Electric.Vehicle.Type Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility
Length:205439 Length:205439 Length:205439 Length:205439
Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character
Electric.Range Base.MSRP Legislative.District DOL.Vehicle.ID Vehicle.Location Electric.Utility
Min. : 0.00 Min. : 0.0 Min. : 1.00 Min. : 4469 Length:205439 Length:205439
1st Qu.: 0.00 1st Qu.: 0.0 1st Qu.:17.00 1st Qu.:193532440 Class :character Class :character
Median : 0.00 Median : 0.0 Median :33.00 Median :238236841 Mode :character Mode :character
Mean : 52.16 Mean : 922.7 Mean :28.97 Mean :227715617
3rd Qu.: 48.00 3rd Qu.: 0.0 3rd Qu.:42.00 3rd Qu.:261871774
Max. :337.00 Max. :845000.0 Max. :49.00 Max. :479254772
NA's :8 NA's :8 NA's :442
X2020.Census.Tract
Min. :1.001e+09
1st Qu.:5.303e+10
Median :5.303e+10
Mean :5.298e+10
3rd Qu.:5.305e+10
Max. :5.602e+10
NA's :3
>
4) So it looks like we did indeed get back a appropriate summary for each column in the data set. It is a very large data set with 48MB of data so it might look a little messy like this, but it works.
5) Our last step is to tell what object oriented system (OO system), an object is associated with, how to determine it, and what the main differences are between a S3 and S4 type object. A S3 object is simple, less formal, with classes not formally defined, a lot more flexible and the methods are operated on the class attribute. S4 objects are a lot more formal and rigorous, with its classes and methods requiring explicit explanation, but they are not very interactive, and does not allow you to easily change the class of existing objects.
6) We can tell weather an object is a S3 type by using the "class" attribute, since S3 objects use the class attribute. We can distinguish S4 objects by using the "isS4" function. It is pretty self explanatory.
7) Now based on our data set lets create a S3 and a S4 type objects.
> #STEP 3
> #explore if s3 and s4 object types can be applied to my data set
> #s3 object test
> class(myDataSet)
[1] "data.frame"
> #s4 object test
> isS4(myDataSet)
[1] FALSE
>
>
>
> #How do you determine the base type (like integer or list) of an object?
> typeof(myDataSet)
[1] "list"
>
> #create two examples of S3 and S4
>
> ### Define a constructor function for S3 object###
> electric_vehicle <- function(make, model, year, type) {
+ obj <- list(make = make, model = model, year = year, type = type)
+ class(obj) <- "electric_vehicle"
+ return(obj)
+ }
>
> # Create an S3 object
> ev1 <- electric_vehicle("Tesla", "Model S", 2022, "BEV")
>
> # Define a method for printing the object
> print.electric_vehicle <- function(ev) {
+ cat("Electric Vehicle:\n")
+ cat("Make:", ev$make, "\nModel:", ev$model, "\nYear:", ev$year, "\nType:", ev$type, "\n")
+ }
>
> # Test the method
> print(ev1)
Electric Vehicle:
Make: Tesla
Model: Model S
Year: 2022
Type: BEV
>
>
> ### Define an S4 class###
> setClass("ElectricVehicle",
+ slots = c(make = "character", model = "character", year = "numeric", type = "character"))
>
> # Create an S4 object
> ev2 <- new("ElectricVehicle", make = "Tesla", model = "Model X", year = 2023, type = "BEV")
>
> # Define a method for showing the object
> setMethod("show", "ElectricVehicle", function(object) {
+ cat("Electric Vehicle:\n")
+ cat("Make:", object@make, "\nModel:", object@model, "\nYear:", object@year, "\nType:", object@type, "\n")
+ })
>
> # Test the method
> show(ev2)
Electric Vehicle:
Make: Tesla
Model: Model X
Year: 2023
Type: BEV
| |
|
8) Looking back at our data set, we can see that myDataSet is a S3 type object and is not a S4, in fact it is a list type object.
9) If you would like to see the R code, uncut then head on over to my GitHub.
Comments
Post a Comment