Search code examples
rbooleancomparisonboolean-logic

Why TRUE == "TRUE" is TRUE in R?


  1. Why TRUE == "TRUE" is TRUE in R?
  2. Is there any equivalent for === in R?

Update:

These are all returning FALSE:

TRUE == "True"
TRUE == "true"
TRUE == "T"

The only TRUE value is TRUE == "TRUE".

In case of checking with identical() everything works fine.

Second Update:

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.


Solution

  • According to the help file ?`==` :

    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.

    So TRUE is coerced to "TRUE" (i. e. as.character(TRUE)), hence the equality.

    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