groupGeneric {base}R Documentation

S3 Group Generic Functions

Description

Group generic methods can be defined for four pre-specified groups of functions, Math, Ops, Summary and Complex. (There are no objects of these names in base R, but there are in the methods package.)

A method defined for an individual member of the group takes precedence over a method defined for the group as a whole.

Usage

## S3 methods for group generics have prototypes:
Math(x, ...)
Ops(e1, e2)
Complex(z)
Summary(..., na.rm = FALSE)

Arguments

x, z, e1, e2 objects.
... further arguments passed to methods.
na.rm logical: should missing values be removed?

Details

There are four groups for which S3 methods can be written, namely the "Math", "Ops", "Summary" and "Complex" groups. These are not R objects, but methods can be supplied for them and base R contains factor, data.frame and difftime methods for the first three groups. (There is also a ordered method for Ops, POSIXt and Date methods for Math and Ops, package_version methods for Ops and Summary, as well as a ts method for Ops in package stats.)

  1. Group "Math":

    Members of this group dispatch on x. Not all members will accept more than one argument: most members have default methods expecting one argument. (Specifically, atan and the gamma functions accept only one argument and log, round and signif accept one or two.)

  2. Group "Ops":

    This group contains both binary and unary operators (+, - and !): when a unary operator is encountered the Ops method is called with one argument and e2 is missing.

    The classes of both arguments are considered in dispatching any member of this group. For each argument its vector of classes is examined to see if there is a matching specific (preferred) or Ops method. If a method is found for just one argument or the same method is found for both, it is used. If different methods are found, there is a warning about ‘incompatible methods’: in that case or if no method is found for either argument the internal method is used.

  3. Group "Summary":

    Members of this group dispatch on the first argument supplied.

  4. Group "Complex":

    Members of this group dispatch on z.

Note that a method will used for either one of these groups or one of its members only if it corresponds to a "class" attribute, as the internal code dispatches on oldClass and not on class. This is for efficiency: having to dispatch on, say, Ops.integer would be too slow.

The number of arguments supplied for primitive members of the "Math" group generic methods is not checked prior to dispatch.

There is no lazy evaluation of arguments for group-generic functions.

Technical Details

The details of method dispatch and variables such as .Generic are discussed in the help for UseMethod. There are a few small differences:

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.

S4groupGeneric for group generics for S4 methods.

Examples

d.fr <- data.frame(x=1:9, y=rnorm(9))
class(1 + d.fr) == "data.frame" ##-- add to d.f. ...

methods("Math")
methods("Ops")
methods("Summary")
methods("Complex")  # none in base R

[Package base version 2.5.0 Index]