Section 14 R Atomic Class: 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
14.1 Examples
<- TRUE
x class(x)
typeof(x)
is.logical(x)
<- T
y class(y)
<- F
z class(z)
is.logical(z)
14.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 |
<- 15
x <- 3
y <- x > y
z
z
<- -3.5
x <- 4
y <- x > y
z
z
<- 'A'
x <- 'a'
y <- x > y
z
z
<- 'A'
x <- 'Z'
y <- x > y
z
z
<- 'z'
x <- 'A'
y <- x > y
z
z
<- 'apple'
x <- 'grape'
y <- x > y
z
z
<- 'grape'
x <- 'banana'
y <- x > y
z
z
<- TRUE
x <- FALSE
y <- x > y
z
z
<- T
x <- F
y <- x > y
z z
14.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 |
<- 10L
x <- 5L
y
<- x + y
z1 <- x - y
z2 <- x
z3
x; y
z1; z2; z3
<- (x > z1) & (x > z2)
m
m
<- (x == z1) & (x < z2)
n
n
<- (y > z1) & (y > z2)
p
p
<- (y >= z1) & (y <= z2)
q
q
<- (y >= z1) | (y <= z2)
r
r
<- (x >= z1) | (y >= z3)
s
s
<- !(x >= z2) | (y >= z3)
t t