57
  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.

Henrik
  • 65,555
  • 14
  • 143
  • 159
Mahdi
  • 9,247
  • 9
  • 53
  • 74
  • Is `===` is the javascript operator? [The Strict Equality Comparison Algorithm?](http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.6) – agstudy Feb 18 '13 at 08:47
  • @agstudy I guess in many C-driven syntax languages, we have this operator for both the value and data-type check at the same time. I'm not sure about other languages ... – Mahdi Feb 18 '13 at 08:49
  • 1
    TRUE==1 and TRUE==1.0 and TRUE==1.0000000000000001 (and 0.99999999999999999) are also true. In general everything that is close enough to 1.0 to be IEEE754-rounded to it. – Bernd Elkemann Feb 18 '13 at 08:55
  • @Mahdi most C-driven languages have static types, so this operator does not make sens for them. Note also that ruby has this operator. So It is mostly used in languages with dynamic typing & default type coercion for value comparison. – Simon Bergot Feb 18 '13 at 12:35
  • Possible duplicate of http://stackoverflow.com/questions/5681166/what-evaluates-to-true-false-in-r – Arun Feb 18 '13 at 20:55
  • @Simon, ruby is very strongly typed and does not have any default type coercion at all (yes, it's dynamically typed, but not weakly typed at all). The "==" operator in ruby *is* the exact equality operator, and if you are using "===" for equality, you are doing things wrong. The "===" operator is only tangentially even related to "==". In ruby, "===" is the case equivalence operator. It says "can the right hand operand 'fit' into the left hand?". It's a little hard to explain without exmaples, so see http://stackoverflow.com/questions/3422223/vs-in-ruby for details. – Ben Lee Feb 20 '13 at 17:21

3 Answers3

62

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
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • Thanks for the answer, `identical` works fine, but `TRUE == "T"` or `TRUE == "true"` is still `FALSE`. Any idea? – Mahdi Feb 18 '13 at 08:36
  • 3
    Ok I have to edit my answer because I thought of it backward: actually `==` coerce to a common type and the order of precedence is character, complex, numeric, integer, logical and raw. So it is actually TRUE that is coerced to "TRUE" and not the opposite, my bad. – plannapus Feb 18 '13 at 08:38
  • 1
    @plannapus maybe you should treat NaN case ..I don't think that identical treats NaN like ===, e.g : NaN===NaN is False bu identical(NaN,NaN) is TRUE. – agstudy Feb 18 '13 at 08:54
  • @agstudy the problem being here that I don't know enough the languages that use `===` to understand why NaN===NaN is FALSE :) – plannapus Feb 18 '13 at 08:56
  • @plannapus maybe the Op can clarify this point by editing his question and adding another tag language which uses this operator.. – agstudy Feb 18 '13 at 08:59
  • 1
    @agstudy you can emulate `NaN===NaN` giving FALSE with argument `single.NA=FALSE` in `identical`: `identical(log(-1), 0*Inf)` gives TRUE but `identical(log(-1), 0*Inf, single.NA=FALSE)` gives FALSE (however just testing `identical(NaN, NaN, single.NA=FALSE)` still gives TRUE). – plannapus Feb 18 '13 at 09:15
  • 2
    In R nothing is "==" to `NA` or `NaN`, even themselves. – IRTFM Feb 18 '13 at 09:15
  • @agstudy Sorry, I can not get the point. Could you tell me what you exactly expect me to add to the original post? – Mahdi Feb 18 '13 at 12:40
  • @Mahdi The part that is missing from the question is what `===` means. Since it's not part of R, we don't know for sure which language's `===` operator you are referring to. – David Heffernan Feb 18 '13 at 13:52
  • @DavidHeffernan You're right! I've updated the question, please check the Second Update. – Mahdi Feb 18 '13 at 15:39
  • @Mahdi Good. Which `===` were you thinking of? From which language? – David Heffernan Feb 18 '13 at 15:42
  • @DavidHeffernan Something like what we have in PHP. I'm wondering is that so different from one language to another one? – Mahdi Feb 18 '13 at 16:05
  • Yeah, the details of these things varies from language to language. Anyway, it's clear now. – David Heffernan Feb 18 '13 at 16:23
12

TRUE and FALSE are reserved words in R. I don't think eznme was correct (before his edit) when he said any non-zero value was TRUE, since TRUE == "A" evaluates to FALSE. (That would have been correct in explaining why TRUE == 1 evaluates to TRUE, but it would not explain the result for TRUE == 7

The explanation given by plannapus was taken out of the context of describing the behavior of as.logical. It is closer to the "truth", because it is the implicit coercion of TRUE to character by the == operator that creates this result. Although T and F are initially given the values of TRUE and FALSE, they can be reassigned to other values or types.

> TRUE == as.logical( c("TRUE", "T", "true", "True") )
[1] TRUE TRUE TRUE TRUE

>  TRUE == 7
[1] FALSE
> TRUE == as.logical(7)
[1] TRUE
>  TRUE == as.logical("A")
[1] NA

(I earlier incorrectly wrote that the coercion induced by TRUE == "TRUE" was to logical; it's actually via as.character(TRUE) returning "TRUE".)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • The previous quote was indeed for `as.logical` because I assumed that `==` was coercing to a common type, I just assumed a wrong order for the precedence. See my edit for the correction. – plannapus Feb 18 '13 at 08:41
  • `since TRUE == "A" evaluates to FALSE` : because they are **different** values! – Bernd Elkemann Feb 18 '13 at 08:41
  • But TRUE and "TRUE" are also different values, ... in fact they are even different modes. – IRTFM Feb 18 '13 at 08:49
3

In addition to

TRUE == "TRUE"

these are also true:

  • TRUE==1
  • TRUE==1.0
  • TRUE==1.0000000000000001
  • TRUE==0.99999999999999999 etc, in general also all values close enough to 1.0 to be IEEE754-rounded to it.

But what is more interesing is what if() checks: it checks non-false; in fact this plots!:

if(4.0) plot(1) 

I think the only values that dont trigger if() are 0, F, FALSE and "FALSE" they seem defined as exactly 0.

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • 2
    ?"if" clearly states that it's arguments are coerced to logical. – IRTFM Feb 18 '13 at 09:14
  • Everything that is non-false will be coerced to true. The datatype-width is not interesting here, it is interesting to note which original values are mapped to true and which to false. – Bernd Elkemann Feb 18 '13 at 09:15
  • 2
    Not really true that all non-FALSE will be coerced to TRUE. NA's will throw an error. – IRTFM Feb 18 '13 at 09:21