logical
Logical Objects

Description

Creates or tests for objects of mode logical.

Usage

logical(length = 0L)
is.logical(x)
as.logical(x, ...)
## S3 method for class 'factor':
as.logical(x, ...)

Arguments

length the desired length for the resulting object.
x any object.
... additional arguments for particular methods.

Details

as.logical is generic and primitive. Only one method for a factor object is implemented at present. There is no default method for generic as.logical.
Logical objects are coerced automatically to numeric for arithmetic and other numeric computations. In the coercion, TRUE becomes 1 and FALSE becomes 0.
Conversely, numeric objects are coerced to logical by setting all non-zero values to TRUE. Note that this practice allows for no rounding error, so it is not a good practice, except in the case where computations are known to have integer results.
When a vector of mode "character" is passed to it, as.logical coerces the character strings "F", "FALSE", "False", and "false" to FALSE; it coerces the character strings "T", "TRUE", "True", and "true" to TRUE; it coerces all other strings to NA.
When a factor is passed to it, as.logical.factor uses levels(x)[x] as input and calls as.logical as usual.
Note the difference between coercing to a simple object of mode "logical" and setting the mode attribute:
The above example changes the mode of myobject, but it leaves all other attributes unchanged (so, for example, a matrix stays a matrix); whereas the value of as.logical(myobject) has no attributes.
Value
logicalreturns a simple object of mode "logical", of the specified length.
is.logicalreturns TRUE if x has mode "logical". Otherwise, it returns FALSE. Its behavior is unaffected by attributes of x; for example, x could be a logical array (in contrast to the behavior of is.vector).
as.logicalreturns x if x is a simple object of mode "logical". Otherwise, it returns a logical object of the same length as x, with data resulting from coercing the elements of x to mode "logical".
See Also
Comparison, .Fortran, Logic, mode.
Examples
zz <- c(-pi, 0.0, pi, NA) # sample object
logical(length(zz)) # logical object same length as zz
as.logical(zz)
as.logical(c("T", "F", "T", "T"))

f <- factor(c("T", "false", "test", "F", "NULL", "TRUE")) as.logical(f)

Package base version 6.0.0-69
Package Index