为什么是TRUE =="TRUE"? R中的TRUE是正确的吗? [英] Why TRUE == "TRUE" is TRUE in R?
问题描述
- 为什么R中的
TRUE == "TRUE"
是TRUE
? - R中的
===
是否有等效项?
- Why
TRUE == "TRUE"
isTRUE
in R? - Is there any equivalent for
===
in R?
更新:
这些都返回FALSE
:
TRUE == "True"
TRUE == "true"
TRUE == "T"
唯一的TRUE
值是TRUE == "TRUE"
.
如果使用identical()
检查,一切正常.
In case of checking with identical()
everything works fine.
第二次更新:
通过===
运算符,我的意思是检查变量的 Value 和数据类型的过程.在这种情况下,我假设==
运算符将只比较变量的 Values ,而不是它们的 Data Type .
By ===
operator I meant the process of checking the Value and the Data Type of a variable. In this case I assumed that the ==
operator will only compare the Values of variables, not their Data Type as well.
推荐答案
根据帮助文件?`==`
:
如果两个参数是不同类型的原子向量,则一个被强制转换为另一个类型,优先级的(降序)顺序为字符,复杂,数字,整数,逻辑和原始.
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)
),因此等于.
So TRUE
is coerced to "TRUE"
(i. e. as.character(TRUE)
), hence the equality.
运算符===
的等效项(即两个对象相等且类型相同)将是函数identical
:
The equivalent of operator ===
(i. e. are the two objects equal and of the same type) would be function identical
:
identical(TRUE, "TRUE")
[1] FALSE
这篇关于为什么是TRUE =="TRUE"? R中的TRUE是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!