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?
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.
if
condition, non-zero numbers are treated like TRUE
and 0 is treated like FALSE
.
Apr 15 '11 at 22:36
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.
TRUE
and FALSE
are reserved. T
and F
can be re-defined. I.e. T <- "hi"
is valid, while TRUE <- "hi"
is an error.
Apr 15 '11 at 19:02
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.
Apr 15 '11 at 19:06
Rprofile
file, you'll see that T
and F
have TRUE
and FALSE
values assigned, accordingly.
if
attempts to coerce the condition to logical (e.g. if("True") print("true!")
). See ?"if"
and ?as.logical
for more information.
Apr 15 '11 at 21:54
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
.
==
if you really want to test for equivalence withTRUE
orFALSE
. If you use it, you are are doing a numerical comparison on the values thatTRUE
andFALSE
coerce to.isTRUE()
andidentical()
should be used if you really wan to know what is or is notTRUE
orFALSE
. See my answer for more.1==TRUE
evaluate toTRUE
as a special case along with0
forFALSE
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.