为什么 TRUE == “TRUE"?在 R 中是 TRUE 吗? [英] Why TRUE == "TRUE" is TRUE in R?

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

问题描述

  1. 为什么 TRUE == "TRUE" 在 R 中是 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.

第二次更新:

=== 运算符是指检查变量数据类型的过程>.在这种情况下,我假设 == 运算符只会比较变量的 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 an operator === in some other language (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天全站免登陆