What's the difference between identical(x, y) and isTRUE(all.equal(x, y))?
Asked Answered
A

3

44

Is there any difference between testing isTRUE(all.equal(x, y)) and identical(x, y)?

The help page says:

Don't use 'all.equal' directly in 'if' expressions — either use 'isTRUE(all.equal(....))' or 'identical' if appropriate.

but that "if appropriate" leaves me in doubt. How do I decide which of the two is appropriate?

Abstractionist answered 3/8, 2010 at 10:38 Comment(0)
M
39

all.equal tests for near equality, while identical is more exact (e.g. it has no tolerance for differences, and it compares storage type). From ?identical:

The function ‘all.equal’ is also sometimes used to test equality this way, but was intended for something different: it allows for small differences in numeric results.

And one reason you would wrap all.equal in isTRUE is because all.equal will report differences rather than simply return FALSE.

Maculate answered 3/8, 2010 at 11:35 Comment(2)
thanks, 'no tolerance' was the key: all.equal(8.0, 9.0, tolerance=1.0)Abstractionist
Another example is that identical() will say ints and longs are different, even if they have the same value, whereas all.equal() will say they're equal.Gorman
D
20

In addition to differences in numerical tolerance and comparison of storage mode, unlike all.equal(), identical also tests equality of associated environments. Regular objects in R don't normally have associated environments -- they are most commonly associated with function and formula (and terms) objects. But to illustrate, I'll give two trivial objects different (newly created) environments:

x <- 2; environment(x) <- new.env()
y <- 2; environment(y) <- new.env()
all.equal(x,y)   ## TRUE
identical(x,y)   ## FALSE

There is an ignore.environment argument:

ignore.environment: logical indicating if their environments should be ignored when comparing closures.

but since it is only applied when comparing closures (i.e. functions), it doesn't make a difference in this case - nor will it make a difference when comparing formulae or terms objects.

Delgadillo answered 11/10, 2015 at 23:54 Comment(0)
M
19

identical is fussier. For example:

> identical(as.double(8), as.integer(8))
[1] FALSE
> all.equal(as.double(8), as.integer(8))
[1] TRUE
> as.double(8) == as.integer(8)
[1] TRUE
Mudskipper answered 3/8, 2010 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.