ashkan ashkan -3 years ago 56
R Question

Why does `as.logical(2)` evaluate to `TRUE`?

I have survey data where a cadre of missing codes are observed, like -9 for non-response, -8 for not applicable, -7 for don't know, and so on. (really, it was a poorly designed survey). TRUE and FALSE are coded as 1 and 0.

I had hoped

as.logical(survResp)
would have automatically coded such values as NA with a "NAs introduced by coercion" error, but it does not. Basically only 0 is coded as FALSE. When other casting functions are used, R is usually smart enough to identify a lack of match and make it missing, like with
as.numeric('not_a_number')

Answer Source

Any none zero numeric is TRUE, because only zero has 0 for all bits, in binary representation of a number.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download