What's the difference in R between identical(x, y) and isTRUE(all.equal(x, y))?

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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?

回答1:

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.



回答2:

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 


回答3:

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 

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!