Section 16 Vector Operation: Logical
A logical vector is
TRUE
orFALSE
It is the R representation of Boolean data
The logical vector is very helpful for comparisons
A logical vector is created by typing TRUE or FALSE (uppercase without the quote marks)
R also assume
T
andF
as shorthand forTRUE
orFALSE
Enclosing
TRUE
orFALSE
by quotes will render it to a character vectorFunction to identify the object type:
class, typeof, mode
Other functions to check the object type as logical:
is.logical
16.2 Examples: comparison
R
uses operators==
,!=
,>
,<
,>=
and<=
for logical comparisons
Operator | Explanation | Example |
---|---|---|
== |
x EQUAL y ? |
x == y |
!= |
x NOT EQUAL y ? |
x != y |
< |
x LESS THAN y ? |
x < y |
<= |
x LESS THAN OR EQUAL y ? |
x <= y |
> |
x GREATER THAN y ? |
x > y |
>= |
x GREATER THAN OR EQUAL y ? |
x >= y |
16.3 Examples: more comparisons
R
uses operators==
,>
,<
,>=
and<=
for comparisonsR
also uses operators!
(NOT),&
(AND) and|
(OR)
Operator | Meaning | X | Y | Decision | Example |
---|---|---|---|---|---|
& |
AND |
TRUE | TRUE | TRUE | X & Y |
& |
AND |
TRUE | FALSE | FALSE | X & Y |
& |
AND |
FALSE | TRUE | FALSE | X & Y |
& |
AND |
FALSE | FALSE | FALSE | X & Y |
| | OR |
TRUE | TRUE | TRUE | X | Y |
| | OR |
TRUE | FALSE | TRUE | X | Y |
| | OR |
FALSE | TRUE | TRUE | X | Y |
| | OR |
FALSE | FALSE | FALSE | X | Y |