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.## S3 methods for group generics have prototypes:
Math(x, \dots)
Ops(e1, e2)
Complex(z)
Summary(\dots, na.rm = FALSE)
.Generic
are discussed in the help for UseMethod
. There are a
few small differences:
Ops
, the object
.Method
is a lengthtwo character vector with elements the
methods selected for the left and right arguments respectively. (If
no method was selected, the corresponding element is ""
.)
.Group
records the group used for dispatch (if
a specific method is used this is ""
).
"Math"
, "Ops"
, "Summary"
and
"Complex"
groups. These are not R objects in base R, 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.) "Math"
:
abs
, sign
, sqrt
,
floor
, ceiling
, trunc
,
round
, signif
exp
, log
, expm1
, log1p
,
cos
, sin
, tan
,
cospi
, sinpi
, tanpi
,
acos
, asin
, atan
cosh
, sinh
, tanh
,
acosh
, asinh
, atanh
lgamma
, gamma
, digamma
, trigamma
cumsum
, cumprod
, cummax
, cummin
x
. Most members accept
only one argument, but members log
, round
and
signif
accept one or two arguments, and trunc
accepts
one or more. "Ops"
:
"+"
, ""
, "*"
, "/"
,
"^"
, "%%"
, "%/%"
"&"
, ""
, "!"
"=="
, "!="
,
"<"
, "<="
, ">="
, ">"
+
,

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. If the members of this group are called as functions, any argument
names are removed to ensure that positional matching is always used. "Summary"
:
all
, any
sum
, prod
min
, max
range
"Complex"
:
Arg
, Conj
, Im
, Mod
, Re
z
.
"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 groupgeneric functions.methods
for methods of noninternal generic functions. S4groupGeneric for group generics for S4 methods.require(utils)
d.fr < data.frame(x = 1:9, y = stats::rnorm(9))
class(1 + d.fr) == "data.frame" ## add to d.f. ...
methods("Math")
methods("Ops")
methods("Summary")
methods("Complex") # none in base R
Run the code above in your browser using DataCamp Workspace