- 为什么 R 中的
TRUE == "TRUE"
是TRUE
? - R 中是否有与
===
等效的内容?
更新:
这些都返回FALSE
:
TRUE == "True"
TRUE == "true"
TRUE == "T"
唯一的 TRUE
值为 TRUE == "TRUE"
。
如果使用 identical()
检查,一切正常。
第二次更新:
通过===
运算符,我指的是检查变量的值和数据类型的过程>。在本例中,我假设 ==
运算符只会比较变量的值,而不是它们的数据类型。
最佳答案
根据帮助文件?`==`
:
If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw.
因此,TRUE
被强制转换为 "TRUE"
(即 as.character(TRUE)
),因此相等。
在其他语言中,运算符===
的等价物(即两个对象是否相等且类型相同)将是函数identical
:
identical(TRUE, "TRUE")
[1] FALSE
关于r - 为什么 TRUE == "TRUE"在 R 中是 TRUE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14932015/