Boolean logic in R revolves around the two logical constants TRUE and FALSE. These values represent truth and falsehood and are essential for decision-making, controlling loops, logical operations, and data manipulation.
What are TRUE and FALSE in R?
- TRUE represents a logical true value.
- FALSE represents a logical false value.
These keywords are case-sensitive and must be written in uppercase. They form the foundation of logical operations in R and are widely used in conditions, loops, and comparisons.
1. Using TRUE and FALSE in Conditional Statements
Boolean values are commonly used to control program flow via if, else if, and else statements.
R
x <- 8
if (x > 5) {
print(TRUE)
} else {
print(FALSE)
}
Output:
[1] TRUE
Explanation: Since x > 5 is TRUE, the print statement inside the if block executes.
2. Using Boolean Values in Loops
Using TRUE
and FALSE
in loops helps control iteration. For example, an infinite loop can be created with TRUE
as the condition, which can be exited with a break
statement.
R
count <- 1
while (TRUE) {
print(count)
count <- count + 1
if (count > 5) {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
3. Using Boolean Values in Arithmetic
In arithmetic expressions, R treats TRUE as 1 and FALSE as 0.
R
print(TRUE + 2)
print(FALSE * 10)
Output:
[1] 3
[1] 0
4. Boolean Values in Vectors
Logical values can be stored in vectors and used for sub setting data efficiently.
R
vec <- c(10, 20, 30, 40)
logical_vec <- vec > 20
print(logical_vec)
print(vec[logical_vec])
Output:
FALSE FALSE TRUE TRUE
30 40
5. Performing Logical Operations in R
R provides operators to perform logical operations on Boolean values:
Operator/Function | Description | Example | Result |
---|
& | Element-wise logical AND | c(TRUE, FALSE) & c(FALSE, TRUE) | FALSE FALSE |
&& | Logical AND (checks only the first element of each) | c(TRUE, FALSE) && c(TRUE, TRUE) | TRUE |
| | Performs a logical OR operation on each element of two logical vectors. | c(TRUE, FALSE, TRUE) | c(FALSE, TRUE, FALSE)
| TRUE TRUE TRUE |
|| | Evaluates only the first element of each logical vector and returns a single TRUE or FALSE. | c(TRUE, FALSE) || c(FALSE, TRUE)
| TRUE |
! | Element-wise logical NOT | !c(TRUE, FALSE, TRUE) | FALSE TRUE FALSE |
xor() | Exclusive OR (function, not an infix operator) | xor(c(TRUE, FALSE), c(FALSE, TRUE)) | TRUE TRUE |
%in% | “Membership” operator: element-wise check if left values are in right set | c(1, 2, 3) %in% c(2, 4) | FALSE TRUE FALSE |
Example Usage
Here we will perform all the above operation in R programming languages.
R
a <- TRUE
b <- FALSE
print(a & b)
print(a | b)
print(!a)
print(a && b)
print(a || b)
Output:
FALSE
TRUE
FALSE
FALSE
TRUE
6. Performing Logical Comparisons in R
Boolean logic often results from comparison operations that return TRUE
or FALSE
:
Operator | Description | Example | Result |
---|
== | Equal to | 5 == 5 | TRUE |
!= | Not equal to | 5 != 3 | TRUE |
< | Less than | 3 < 5 | TRUE |
> | Greater than | 5 > 3 | TRUE |
<= | Less than or equal to | 3 <= 3 | TRUE |
>= | Greater than or equal | 4 >= 5 | FALSE |
Example Usage:
Here we will perform all the above operation in R programming languages.
R
a <- 5
b <- 3
print(a == b)
print(a != b)
print(a < b)
print(a > b)
print(a <= b)
print(a >= b)
Output:
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE