gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:57:00 27 4
gpt4 key购买 nike

  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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com