46

For example, in Ruby, only nil and false are false. What is what in R?

e.g.: 5==TRUE and 5==FALSE both evaluate to FALSE. However, 1==TRUE is TRUE. Is there any general rule as to what (objects, numbers, etc.) evaluate to?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Ruby noob
  • 467
  • 1
  • 4
  • 5
  • 4
    Don't use `==` if you _really_ want to test for equivalence with `TRUE` or `FALSE`. If you use it, you are are doing a numerical comparison on the values that `TRUE` and `FALSE` coerce to. `isTRUE()` and `identical()` should be used if you really wan to know what is or is not `TRUE` or `FALSE`. See my answer for more. – Gavin Simpson Apr 15 '11 at 19:29
  • Having `1==TRUE` evaluate to `TRUE` as a special case along with `0` for `FALSE` is a general rule in many programming languages, not just R. Possible reasons for this are the lack of a boolean type in C (the integers 0 and 1 are commonly substituted), and at a more basic level the fact that a single binary digit can represent a boolean value. – Sharpie Apr 17 '11 at 00:38

3 Answers3

57

This is documented on ?logical. The pertinent section of which is:

Details:

     ‘TRUE’ and ‘FALSE’ are reserved words denoting logical constants
     in the R language, whereas ‘T’ and ‘F’ are global variables whose
     initial values set to these.  All four are ‘logical(1)’ vectors.

     Logical vectors are coerced to integer vectors in contexts where a
     numerical value is required, with ‘TRUE’ being mapped to ‘1L’,
     ‘FALSE’ to ‘0L’ and ‘NA’ to ‘NA_integer_’.

The second paragraph there explains the behaviour you are seeing, namely 5 == 1L and 5 == 0L respectively, which should both return FALSE, where as 1 == 1L and 0 == 0L should be TRUE for 1 == TRUE and 0 == FALSE respectively. I believe these are not testing what you want them to test; the comparison is on the basis of the numerical representation of TRUE and FALSE in R, i.e. what numeric values they take when coerced to numeric.

However, only TRUE is guaranteed to the be TRUE:

> isTRUE(TRUE)
[1] TRUE
> isTRUE(1)
[1] FALSE
> isTRUE(T)
[1] TRUE
> T <- 2
> isTRUE(T)
[1] FALSE

isTRUE is a wrapper for identical(x, TRUE), and from ?isTRUE we note:

Details:
....

     ‘isTRUE(x)’ is an abbreviation of ‘identical(TRUE, x)’, and so is
     true if and only if ‘x’ is a length-one logical vector whose only
     element is ‘TRUE’ and which has no attributes (not even names).

So by the same virtue, only FALSE is guaranteed to be exactly equal to FALSE.

> identical(F, FALSE)
[1] TRUE
> identical(0, FALSE)
[1] FALSE
> F <- "hello"
> identical(F, FALSE)
[1] FALSE

If this concerns you, always use isTRUE() or identical(x, FALSE) to check for equivalence with TRUE and FALSE respectively. == is not doing what you think it is.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
5

T and TRUE are True, F and FALSE are False. T and F can be redefined, however, so you should only rely upon TRUE and FALSE. If you compare 0 to FALSE and 1 to TRUE, you will find that they are equal as well, so you might consider them to be True and False as well.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 5
    It's worth noting that only `TRUE` and `FALSE` are reserved. `T` and `F` can be re-defined. I.e. `T <- "hi"` is valid, while `TRUE <- "hi"` is an error. – Joshua Ulrich Apr 15 '11 at 19:02
  • Actually I was asking more about everything else. For example, `1==TRUE` evaluates to TRUE, but `5==TRUE` and `5==FALSE` both evaluate to FALSE. I can't find any documentation on what evaluates to true or false in general. – Ruby noob Apr 15 '11 at 19:06
  • Actually, if you open your global `Rprofile` file, you'll see that `T` and `F` have `TRUE` and `FALSE` values assigned, accordingly. – aL3xa Apr 15 '11 at 19:09
  • @Ruby noob, and `0 == FALSE` evaluates to `TRUE`. =) – aL3xa Apr 15 '11 at 19:11
  • It's worth further noting that the opposite is true in S, where T and F are the reserved words and TRUE and FALSE can be redefined. So it's best to use TRUE and FALSE always in R, and to not reassign T and F. – Ari B. Friedman Apr 15 '11 at 20:47
  • Apearently any integer other then 0 is also evaluated as true: `if (5) print("true!")` – Sacha Epskamp Apr 15 '11 at 21:36
  • 2
    @Sacha: it's not just integers other than 0, and it's not because of evaluation (strictly speaking). It's because `if` attempts to coerce the condition to logical (e.g. `if("True") print("true!")`). See `?"if"` and `?as.logical` for more information. – Joshua Ulrich Apr 15 '11 at 21:54
-1

If you think about it, comparing numbers to logical statements doesn't make much sense. However, since 0 is often associated with "Off" or "False" and 1 with "On" or "True", R has decided to allow 1 == TRUE and 0 == FALSE to both be true. Any other numeric-to-boolean comparison should yield false, unless it's something like 3 - 2 == TRUE.

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107