R Language Coercion


To coerce a variable to a logical use the as.logical() function.

> x <- 2
> z <- x > 4
> z
> class(x)
[1] "numeric"
> as.logical(2)
[1] TRUE

When applying as.numeric() to a logical, a double will be returned. NA is a logical value and a logical operator with an NA will return NA if the outcome is ambiguous.