tags:

views:

66

answers:

2

suddenly a doubt...

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?

+3  A: 

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.

Joshua Ulrich
thanks, 'no tolerance' was the key: `all.equal(8.0, 9.0, tolerance=1.0)`
mariotomo
+1  A: 

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
nullglob
It looks like `identical` checks the class/type of variable (from the help page examples): `identical(1, 1.) ## TRUE in R (both are stored as doubles)``identical(1, as.integer(1)) ## FALSE, stored as different types`
richardh