Greetings world (or just Dr. Friedman),
In R class this week we learned some more about R objects and programming with objects. We took a look at S3 and S4 classes, new terms to me. S3 is informal and has been in R since the beginning of the R language. S3 is simpler, not as rigorous, but is "not as safe" as S4. Class reassignment is more difficult in S4 as well. Most of the material this week was new to me. Although the concepts are not trivial, I think I have a hold on it (mostly!). I have enjoyed learning about classes and generic functions because I feel that I better understand how R works behind the scenes. I learned from the lecture and the text book how to use methods() and make my own generic function. However, I think I need a lot of practice on the latter. Hadley Wickham has an informative post in Advanced R, “OO R”.
I'll use a dataset on invasive iguanas to answer the question, which includes an arbitrarily assigned ID, collection date (M/D/Y format), sex, length, mass, and various other morphological features.
iguana <- read.csv(“iguana.csv”)
Using some functions, we can classify the data:
> isS4(iguana)
[1] FALSE
[1] FALSE
> mode(iguana)
[1] "list"
[1] "list"
> typeof(iguana)
[1] "list"
[1] "list"
> class(iguana)
[1] "data.frame"
> is.data.frame(iguana)[1] "data.frame"
[1] TRUE
> ismatrix(iguana)
[1] FALSE
[1] FALSE
"iguana" is not S4 class, mode() and typeof() return list whereas class() returns data.frame. As it is a data frame, generic functions for this class will work.
I do not think I have seen an S4 object, as every dataset I have used is subset with "$" rather than "@". I will explore further.
No comments:
Post a Comment