为什么是TRUE =="TRUE"? R中的TRUE是正确的吗? [英] Why TRUE == "TRUE" is TRUE in R?

查看:369
本文介绍了为什么是TRUE =="TRUE"? R中的TRUE是正确的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 为什么R中的TRUE == "TRUE"TRUE?
  2. R中的===是否有等效项?
  1. Why TRUE == "TRUE" is TRUE in R?
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆