Test if Two Objects are (Nearly) Equal

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. Don't use all.equal directly in if expressions---either use isTRUE(all.equal(....)) or identical if appropriate.

utilities, arith, programming, logic
all.equal(target, current, ...)
"all.equal"(target, current, tolerance = .Machine$double.eps ^ 0.5, scale = NULL, check.attributes = TRUE, ...)
attr.all.equal(target, current, check.attributes = TRUE, check.names = TRUE, ...)
R object.
other R object, to be compared with target.
Further arguments for different methods, notably the following two, for numerical comparison:
numeric $\ge$ 0. Differences smaller than tolerance are not considered.
numeric scalar > 0 (or NULL). See ‘Details’.
logical indicating if the attributes of target and current (other than the names) should be compared.
logical indicating if the names(.) of target and current should be compared.

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.

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 method for the date-time class "POSIXct" by default allows a tolerance of tolerance = 0.001 seconds.

attr.all.equal is used for comparing attributes, returning NULL or a character vector.


Either TRUE (NULL for attr.all.equal) or a vector of mode "character" describing the differences between target and current.


Arguments other than target and current should be specified via their full name: this will be required as from R 3.1.0.


Chambers, J. M. (1998) Programming with Data. A Guide to the S Language. Springer (for =).

See Also

identical, isTRUE, ==, and all for exact equality testing.

  • all.equal
  • all.equal.default
  • all.equal.numeric
  • all.equal.character
  • all.equal.factor
  • all.equal.formula
  • all.equal.list
  • all.equal.language
  • all.equal.POSIXct
  • all.equal.raw
  • attr.all.equal
library(base) 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
Documentation reproduced from package base, version 3.0.3, License: Part of R 3.0.3

Community examples at Feb 28, 2020 base v3.6.2

**Prompting user to enter numeric values separated by a single space:** ``` prompt <- "Enter the number(s) (Use single space to separate input(s)):" n1 <- as.numeric(strsplit(readline(prompt), " ")[[1]]) n2 <- as.numeric(strsplit(readline(prompt), " ")[[1]]) ``` **Checking whether all inputs in vector n1(target) and vector n2(current) are of type numeric and returning a boolean value:** ``` if(isTRUE(all.equal.numeric(n1,n2,check.attributes = TRUE))) { print("All elements are of type numeric") } else { print("Differences found:") print(all.equal.numeric(n1,n2,check.attributes = TRUE)) } ```