| groupGeneric {base} | R Documentation |
Group Generic Functions
Description
Group generic functions can be defined with either S3 and S4 methods (with different groups). Methods are defined for the group of functions as a whole.
A method defined for an individual member of the group takes precedence over a method defined for the group as a whole.
When package methods is attached there are objects visible with
the names of the group generics: these functions should never be
called directly (a suitable error message will result if they are).
Usage
S3 methods have prototypes:
## S3 method for class 'data.frame'
Math(x, ...)
## S3 method for class 'data.frame'
Ops(e1, e2)
## S3 method for class 'data.frame'
Summary(x, ...)
S4 methods have prototypes:
Arith(e1, e2)
Compare(e1, e2)
Ops(e1, e2)
Math(x)
Math2(x, digits)
Summary(x, ..., na.rm = FALSE)
Complex(z)
Arguments
x, z, e1, e2 |
objects. |
digits |
number of digits to be used in |
... |
further arguments passed to or from methods. |
na.rm |
logical: should missing values be removed? |
S3 Group Dispatching
There are three groups for which S3 methods can be written, namely
the "Math", "Ops" and "Summary" groups. These
are not R objects, but methods can be supplied for them and base R
contain factor and data.frame methods.
(There is also a ordered method for Ops.)
Group
"Math":-
abs,sign,sqrt,
floor,ceiling,trunc,
round,signif -
exp,log,
cos,sin,tan,
acos,asin,atancosh,sinh,tanh,
acosh,asinh,atanh -
lgamma,gamma,gammaCody,
digamma,trigamma,tetragamma,pentagamma -
cumsum,cumprod,cummax,cummin
-
Group
"Ops":-
"+","-","*","/","^","%%","%/%" -
"&","|","!" -
"==","!=","<","<=",">=",">"
-
Group
"Summary":-
all,any -
sum,prod -
min,max -
range
-
The number of arguments supplied for "Math" group generic
methods is not checked prior to dispatch. (Prior to R 1.7.0, all
those whose default method has one argument were checked, but the
others were not.)
S4 Group Dispatching
When package methods is attached, formal (S4) methods can be
defined for groups.
The functions belonging to the various groups are as follows:
Arith"+","-","*","^","%%","%/%","/"Compare"==",">","<","!=","<=",">="Ops"Arith","Compare"Math"log","sqrt","log10","cumprod","abs","acos","acosh","asin","asinh","atan","atanh","ceiling","cos","cosh","cumsum","exp","floor","gamma","lgamma","sin","sinh","tan","tanh","trunc"Math2"round","signif"Summary"max","min","range","prod","sum","any","all"Complex"Arg","Conj","Im","Mod","Re"
Functions with the group names exist in the methods package but
should not be called directly.
All the functions in these groups (other than the group generics themselves) are basic functions in R. They are not by default S4 generic functions, and many of them are defined as primitives, meaning that they do not have formal arguments. However, you can still define formal methods for them. The effect of doing so is to create an S4 generic function with the appropriate arguments, in the environment where the method definition is to be stored. It all works more or less as you might expect, admittedly via a bit of trickery in the background.
References
Appendix A, Classes and Methods of
Chambers, J. M. and Hastie, T. J. eds (1992)
Statistical Models in S.
Wadsworth & Brooks/Cole.
See Also
methods for methods of non-Internal generic functions.
Examples
methods("Math")
methods("Ops")
methods("Summary")
d.fr <- data.frame(x=1:9, y=rnorm(9))
data.class(1 + d.fr) == "data.frame" ##-- add to d.f. ...