all.equal(x, y)
is a utility to compare R objects x
and y
testing near equality. If they are different,
comparison is still made to some extent, and a report of the
differences is returned. Do not use all.equal
directly in
if
expressions---either use isTRUE(all.equal(....))
or
identical
if appropriate.
all.equal(target, current, ...)
"all.equal"(target, current, tolerance = .Machine$double.eps ^ 0.5, scale = NULL, ..., check.attributes = TRUE)
"all.equal"(target, current, ..., check.attributes = TRUE, use.names = TRUE)
"all.equal"(target, current, ..., tolerance = 1e-3, scale)
attr.all.equal(target, current, ..., check.attributes = TRUE, check.names = TRUE)
target
.tolerance
are not reported. The default value is close to
1.5e-8
.NULL
). See Details.attributes
of target
and current
(other than the names) should be compared.list
comparison
should report differing components by name (if matching) instead of
integer index. Note that this comes after ...
and so must
be specified by its full name.names(.)
of target
and current
should be compared.TRUE
(NULL
for attr.all.equal
) or a vector
of mode
"character"
describing the differences
between target
and current
.
all.equal
is a generic function, dispatching methods on the
target
argument. To see the available methods, use
methods("all.equal")
, but note that the default method
also does some dispatching, e.g. using the raw method for logical
targets.
Remember that arguments which follow ...
must be specified by
(unabbreviated) name: some of them were before ...
prior to
R 3.1.0. It is inadvisable to pass unnamed arguments in ...
as these will match different arguments in different methods.
Numerical comparisons for scale = NULL
(the default) are done
by first computing the mean absolute difference of the two numerical
vectors. If this is smaller than tolerance
or not finite,
absolute differences are used, otherwise relative differences scaled
by the mean absolute difference. If scale
is positive, absolute comparisons are made after
scaling (dividing) by scale
.
For complex target
, the modulus (Mod
) of the
difference is used: all.equal.numeric
is called so arguments
tolerance
and scale
are available.
The list
method compares components of
target
and current
recursively, passing all other
arguments, as long as both are list-like, i.e., fulfill
either is.vector
or is.list
.
The methods for the date-time classes by default allow a tolerance of
tolerance = 0.001
seconds, and ignore scale
.
attr.all.equal
is used for comparing
attributes
, returning NULL
or a
character
vector.
=
).
identical
, isTRUE
, ==
, and
all
for exact equality testing.
all.equal(pi, 355/113)
# not precise enough (default tol) > relative error
d45 <- pi*(1/4 + 1:10)
stopifnot(
all.equal(tan(d45), rep(1, 10))) # TRUE, but
all (tan(d45) == rep(1, 10)) # FALSE, since not exactly
all.equal(tan(d45), rep(1, 10), tolerance = 0) # to see difference
Run the code above in your browser using DataLab