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

后端 未结 3 505
渐次进展
渐次进展 2020-12-02 20:28

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

The help page says:

Don\'t use

3条回答
  •  日久生厌
    2020-12-02 20:49

    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.

提交回复
热议问题