r - 为什么 TRUE == "TRUE"在 R 中是 TRUE?

标签 r boolean comparison boolean-logic

  1. 为什么 R 中的 TRUE == "TRUE"TRUE
  2. 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/

相关文章:

python - Pandas boolean 索引问题

java - 使用 if 语句为变量赋值

perl - "0 but true"在 Perl 中意味着什么?

delphi - 比较 JPEG 图像最快的解决方案是什么? (忽略元数据,仅 "pixels")

python - 如何在 python 的断言语句中使用小于等于

R:将数据框减少为跨两列完全匹配的行

重新编码字符串时,Car 包中的重新编码返回意外符号

python - 相关词的概率计数/频率?

r - 在 RStudio 中使用 Knit PDF 时如何更改纸张大小?

r - R中的地理热图