Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between different types of functions in R

Tags:

function

r

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:

  1. Either Generic or Method: based on the class of the input argument(s), generic functions by using Method Dispatch call an appropriate method function.

  2. Invisible vs. Visible

  3. 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.

like image 300
Sam Avatar asked Jul 31 '12 02:07

Sam


1 Answers

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.

like image 50
emhart Avatar answered Sep 22 '22 00:09

emhart