1 == TRUE # TRUE
as.logical(1) # TRUE
0 == FALSE # TRUE
as.logical(0) # FALSE
2 == TRUE # FALSE
as.logical(2) # TRUE
1 == TRUE # TRUE
as.logical(1) # TRUE
0 == FALSE # TRUE
as.logical(0) # FALSE
2 == TRUE # FALSE
as.logical(2) # TRUE
TRUE and FALSE are 1 and 0, while `as.logical` will transform your value to the "closest" of those two.
If you are used to Pythons truthiness, that is what `as.logical` is similar to.
>>> True + True
2
>>> True * 13 + (1 - False) * 17
30(The same happens in R, for what it's worth. as.integer(TRUE) is one and as.integer(FALSE) is zero and the operations you wrote work just he same.)
But it is an issue, and high level languages shouldn't just replicate it. In a high-level language `1 == TRUE` should be either an error or false.
F:volcano
(from https://codegolf.stackexchange.com/a/219617)https://www.rdocumentation.org/packages/raster/versions/3.5-...
> Change values of a Raster* object to logical or integer values. With as.logical, zero becomes FALSE, all other values become TRUE. With as.integer values are truncated.
2 == TRUE
as
as.logical(2)==TRUE
rather than as
2==as.integer(TRUE)
[Edit: "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."]