R: Test if Two Objects are (Nearly) Equal

all.equal {base} | R Documentation

## Test if Two Objects are (Nearly) Equal

### Description

`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 `identical` or combine the two, as shown in the documentation for `identical`.

### Usage

    all.equal(target, current, ...)

    all.equal.numeric(target, current,
                      tolerance= .Machine$double.eps ^ 0.5, scale=NULL, ...)

### Arguments

`target` | R object.
`current` | other R object, to be compared with `target`.
`...` | Further arguments for different methods, notably the following two, for numerical comparison:
`tolerance` | numeric >= 0. Differences smaller than `tolerance` are not considered.
`scale` | numeric scalar > 0 (or `NULL`). See Details.

### Details

There are several methods available, most of which are dispatched by the default method, see `methods("all.equal")`. `all.equal.list` and `all.equal.language` provide comparison of recursive objects.

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, comparisons are after scaling by `scale`.

For complex arguments, `Mod` of difference is used.

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

### Value

Either `TRUE` or a vector of `mode` `"character"` describing the differences between `target` and `current`.
Numerical differences are reported by relative error

### See Also

`==`, and `all` for exact equality testing.

### Examples

    all.equal(pi, 355/113) # not precise enough (default tol) > relative error

    stopifnot(
    all.equal(gamma(2:14),   cumprod(1:13))) # TRUE, but

    all      (gamma(2:14) == cumprod(1:13)) # FALSE, since not exactly
    all.equal(gamma(2:14),   cumprod(1:13), tol=0) # to see difference

    all.equal(options(), .Options)
    all.equal(options(), as.list(.Options))# TRUE
    .Options $ myopt <- TRUE
    all.equal(options(), as.list(.Options))
    rm(.Options)

[Package Contents]