Since all values other than 0 are taken as true in R, isTRUE(3) should logically evaluate to True but it doesn't. Why so? Also, I would like to know the reason behind isTRUE(NA) being evaluated to false.
-
https://stackoverflow.com/questions/5681166/what-evaluates-to-true-false-in-r, plus [this comment](https://stackoverflow.com/questions/5681166/what-evaluates-to-true-false-in-r#comment6489046_5681184). – Ry- May 10 '18 at 04:07
-
1Note sure if it is useful to you, but note that `isTRUE(as.logical(3))` does evaluate to `TRUE`. – scottkosty May 10 '18 at 04:11
-
@scottkosty Thanks for the information and yeah it does, and the reason i figured out is because as.logical() converts the numeric type to a logical type, hence converting 3 to TRUE. – PeoplePersonsPaperPeople May 10 '18 at 07:41
-
From R-help: `isTRUE(x) is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }`. Before R v. 3.5 (23apr2018) it was `identical(x, TRUE)`. – Rasmus Larsen Mar 16 '23 at 10:06
2 Answers
Straight from the documentation (try ?isTRUE
)
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).
It's not just doing a check on value, it's doing a check to ensure it is a logical value.
I know in computer science often 0 is false and anything non-zero is true, but R approaches things from a statistics point of view, not a computer science point of view, so it's a bit stricter about the definition.
Saying this, you'll notice this if statement evaluates the way you would imagine
if(3){print("yay")}else{print("boo")}
It's just the way R going about evaluation. The function isTRUE
is just more specific.
Also note these behaviours
FALSE == 0
is true
TRUE == 1
is true
TRUE == 3
is false
So R will treat 0 and 1 as false and true respectively when perform these sorts of evaluations.
I'm not sure what your planned implementation was (if there was any) but it's probably better trying to be precise about boolean logic in R, or test things beforehand.
As for NA, more strange behaviour.
TRUE & NA
equates to NA
TRUE | NA
equates to TRUE
In these cases R forces NA to a logical type, since anything or'd with TRUE is a TRUE, it can equate that. But the value would change depending on the second term in an and operation, so it returns NA.
As for your particular case, again isTRUE(NA)
is equated as false because NA is not a length-one logical vector whose only element is TRUE.

- 1,152
- 8
- 14
-
I checked the documentation for isTRUE and it says this: isTRUE(x) is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }. Is the definition you provided [isTRUE is an abbreviation of identical(TRUE, x)] EXACTLY the same as this one? I specifically do not get the 'no attributes(not even names)' part of your definition. Can you please explain it a little bit more? I am a beginner in R and as you did guess, there wasn't any planned implementation. I'm just trying to study these functions in depth. Thanks!! – PeoplePersonsPaperPeople May 10 '18 at 07:39
-
Hey! The definition you're saying I provided actually also came from the documentation, and is identical to what you're saying :) When you use isTRUE it checks 4 things 1. Is the data type logical? (aka boolean, aka TRUE/FALSE) 2. Is it of length 1? (aka is it a vector with only one element) 3. Is it a non-NA value? (aka does it have a value?) 4. Is its value TRUE? It does all these checks because as I showed, there's a bunch of things that can look like TRUE but not be a boolean TRUE.... – LachlanO May 10 '18 at 22:44
-
To elaborate: 1 can equate as a TRUE. In some circumstances any non-zero can. It's situational as to when R promotes to a logical value and when it doesnt. as @scottkosty comments if you use `isTRUE(as.logical(3))` this equates to true, because the `as.logical` part converts the integer into a boolean value, so now it passes check number 1 (is it a logical type). The 'no attributes (not even names)' part is being more specific about the length of the vector, in R you can assign attributes such as names to objects, this is just being very clear. You can ONLY have a single logical element. – LachlanO May 10 '18 at 22:44
Because this function bypass R's automatic conversion rules and check that x is literally TRUE or FALSE.

- 3,744
- 4
- 33
- 47