R’s all.equal, relative vs absolute

The all.equal() function is useful to comparing whether two numeric objects are equivalent, but it has a weird quirk.

> all.equal(4, 5)
[1] "Mean relative difference: 0.25"

You might expect that the difference should be 1. But note the “relative difference.” The default for all.equal is to report the difference for a call of all.equal(x, y) as |x - y|/x. This also means that all.equal(x, y) is not going to report the same difference as all.equal(y, x).

The scale= argument makes all.equal instead report absolute difference:

> all.equal(4, 5, scale = 1)
[1] "Mean absolute difference: 1"

This comes in especially with the tolerance= argument. Let’s say you want to consider two values “equal” if they’re less than .5 apart.

> all.equal(4, 4.4, tolerance = .5)
[1] TRUE
> all.equal(4, 5, tolerance = .5)
[1] TRUE

That second one is clearly wrong. However, if we set the scale= argument to 1, the tolerance is applied on the absolute scale instead.

> all.equal(4, 5, tolerance = .5, scale = 1)
[1] "Mean absolute difference: 1"
Home | Back to blog

This work is licensed under CC BY-NC 4.0 Creative Commons BY-NC image