相同(x,y)和isTRUE(all.equal(x,y))之间R的差异是什么?

时间:2021-05-05 11:29:46

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

测试isTRUE(all.equal(x,y))和相同(x,y)之间有什么区别吗?

The help page says:

帮助页面说:

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

不要在'if'表达式中直接使用'all.equal' - 如果合适,使用'isTRUE(all.equal(....))'或'same'。

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

但“如果合适的话”让我怀疑。我如何确定哪两个合适?

3 个解决方案

#1


21  

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:

all.equal测试接近相等,而相同则更精确(例如,它不具有差异容差,并且它比较存储类型)。来自?相同:

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.

函数'all.equal'有时也用于以这种方式测试相等性,但是用于不同的东西:它允许数值结果的微小差异。

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

你在isTRUE中包装all.equal的一个原因是因为all.equal将报告差异而不是简单地返回FALSE。

#2


12  

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


9  

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:

除了数值容差的差异和存储模式的比较之外,与all.equal()不同,相同的也测试相关环境的相等性。 R中的常规对象通常不具有关联环境 - 它们通常与函数和公式(和术语)对象相关联。但为了说明,我将给出两个不同的(新创建的)环境:

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参数:

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

ignore.environment:指示在比较闭包时是否应忽略其环境的逻辑。

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.

但由于它仅在比较闭包(即函数)时应用,因此在这种情况下不会产生差异 - 在比较公式或术语对象时也不会产生差异。

#1


21  

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:

all.equal测试接近相等,而相同则更精确(例如,它不具有差异容差,并且它比较存储类型)。来自?相同:

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.

函数'all.equal'有时也用于以这种方式测试相等性,但是用于不同的东西:它允许数值结果的微小差异。

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

你在isTRUE中包装all.equal的一个原因是因为all.equal将报告差异而不是简单地返回FALSE。

#2


12  

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


9  

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:

除了数值容差的差异和存储模式的比较之外,与all.equal()不同,相同的也测试相关环境的相等性。 R中的常规对象通常不具有关联环境 - 它们通常与函数和公式(和术语)对象相关联。但为了说明,我将给出两个不同的(新创建的)环境:

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参数:

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

ignore.environment:指示在比较闭包时是否应忽略其环境的逻辑。

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.

但由于它仅在比较闭包(即函数)时应用,因此在这种情况下不会产生差异 - 在比较公式或术语对象时也不会产生差异。