r - 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 位。
解决方案
它们并不完全相同,因为它们具有不同的类型。如果您查看文档,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
输出向量。
typeof
并storage.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
推荐阅读
- php - 如何在 Laravel 过去 30 天的愤怒中每天获得“付款”的总和
- sql - 如何在 PostgreSQL 中应用 CROSS JOIN?
- fipy - 一项取决于两个不同方程的两个变量的 FiPy 传输反应
- python - 多级饼图散景
- javascript - Chrome 调试:在节点内容值更改上放置 JS 断点
- google-sheets - Google表格不同的2个帐户之间的右键菜单
- apache-spark - Delta Table 是否非常适合不断变化的实体?
- javascript - 如果条件在功能组件中使用旧状态
- android - ndk-build, C:\User\MyUser 未被识别为内部或外部命令
- django - django-allauth 自定义注册表单在添加新用户时在管理页面中引发错误。'CustomUserForm' 对象没有属性 'instance'