I would appreciate help understanding the main differences between several types of functions in R.
I'm somewhat overwhelmed among the definitions of different types of functions and it has become somewhat difficult to understand how different types of functions might relate to each other.
Specifically, I'm confused about the relationships and differences between the following types of functions:
Either Generic or Method: based on the class of the input argument(s), generic functions by using Method Dispatch call an appropriate method function.
Invisible vs. Visible
Primitive vs. Internal
I'm confused about how these different types of functions relate to each other (if at all) and what the various differences and overlaps are between them.
Here's some documentation about primitive vs internal: http://www.biosino.org/R/R-doc/R-ints/_002eInternal-vs-_002ePrimitive.html
Generics are generic functions that can be applied to a class object. Each class is written with specific methods that are then set as generic. So you can see the specific methods associated with a generic call with the "methods" function:
methods(print)
This will list all the methods associated with the generic "print". Alternatively you can see all the generics that a given class has with this call
methods(,"lm")
Where lm is the class linear model. Here's an example:
x <- rnorm(100)
y <- 1 + .4*x + rnorm(100,0,.1)
mod1 <- lm(y~x)
print(mod1)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
1.002 0.378
print.lm(mod1)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
1.002 0.378
Both the print(mod1) (the generic call) and print.lm(mod1) (the method call to the class) do the same thing. Why does R do this? I don't really know, but that's the difference between method and generic as I understand it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With