首页 > 解决方案 > R中“相同()”函数的问题?“相同()”如何适用于不同类型的对象?

问题描述

(添加了可重复的示例)

我无法理解为什么以下是FALSE(我知道它们是double并且integer分别):

identical(1, as.integer(1)) # FALSE

?identical揭示:

num.eq:逻辑指示是否应该使用 == ('equal') 或按位比较来比较(双精度和复数非 NA)数字。后者(非默认)区分 -0 和 +0。

sprintf("%.8190f", as.integer(1))sprintf("%.8190f", 1)返回完全相同的位模式。所以,我认为以下至少一项必须返回TRUE。但是,我得到FALSE以下每一项:

identical(1, as.integer(1), num.eq=TRUE) # FALSE
identical(1, as.integer(1), num.eq=FALSE) # FALSE

我现在这样考虑:如果sprintf是符号指标,而不是存储指标,那么这意味着identical()基于存储进行比较。即 identical(bitpattern1, bitpattern1bitpattern2)返回FALSE。我找不到上述FALSE/FALSE情况的任何其他合乎逻辑的解释。

我确实知道在 R 的 32 位/64 位架构中,整数都存储为 32 位。

标签: rintegerdoublestoragenotation

解决方案


它们并不完全相同,因为它们具有不同的类型。如果您查看文档,identical您会发现identical(1, as.integer(1))带有注释的示例## FALSE, stored as different types。这是一个线索。R语言的定义提醒我们:

单个数字,例如 4.2,和字符串,例如“四点二”,仍然是长度为 1 的向量;没有更多的基本类型(强调我的)。

所以,基本上所有东西都是一个带有类型的向量(这也是为什么[1]每次 R 返回某些东西时都会出现的原因)。您可以通过使用 显式创建长度为 1 的向量来检查这一点vector,然后将其与 进行比较0

x <- vector("double", 1)
identical(x, 0)
# [1] TRUE

也就是说,“double”类型和长度 == 1 的输出向量都vector("double", 1)0输出向量。

typeofstorage.mode指向同一件事,所以当您说“这意味着identical()基于存储进行比较”时,您是对的。我认为这不一定意味着正在比较“位模式”,尽管我认为这是可能的。查看使用以下命令更改存储模式时会发生什么storage.mode

## Assign integer to x. This is really a vector length == 1.
x <- 1L

typeof(x)
# [1] "integer"

identical(x, 1L)
# [1] TRUE

## Now change the storage mode and compare again. 
storage.mode(x) <- "double"

typeof(x)
# [1] "double"

identical(x, 1L) # This is no longer TRUE.
# [1] FALSE

identical(x, 1.0) # But this is.
# [1] TRUE

最后一点:identical州的文档num.eq是……</p>

逻辑指示是否应该使用 == ('equal') 或按位比较来比较(双精度和非 NA 复数)数字。

因此,更改num.eq不会影响任何涉及整数的比较。尝试以下操作:

# Comparing integers with integers.
identical(+0L, -0L, num.eq = T) # TRUE
identical(+0L, -0L, num.eq = F) # TRUE

# Comparing integers with doubles.
identical(+0, -0L, num.eq = T) # FALSE
identical(+0, -0L, num.eq = F) # FALSE

# Comparing doubles with doubles.
identical(+0.0, -0.0, num.eq = T) # TRUE
identical(+0.0, -0.0, num.eq = F) # FALSE


推荐阅读