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
Similar Reads
Bitwise Logical Operations in R Bitwise logical operations are used to perform logical operations bit by bit in a number. This article discusses how bitwise logical operators are used in the R programming language. The logical operations include: OPERATORDESCRIPTIONSYNTAXbitwOrbitwise ORbitwOr(value1,value2)bitwXorbitwise XORbitwX
4 min read
R If Else Conditions The if-statement in Programming Language alone tells us that if a condition is true it will execute a block of statements and if the condition is false it wonât. But what if we want to do something else if the condition is false? Here comes the R Programming Language else statement. We can use the e
5 min read
R Operators Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R supports majorly four kinds
5 min read
How to Code in R programming? R is a powerful programming language and environment for statistical computing and graphics. Whether you're a data scientist, statistician, researcher, or enthusiast, learning R programming opens up a world of possibilities for data analysis, visualization, and modeling. This comprehensive guide aim
4 min read
Basic Syntax in R Programming R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its features. This article assu
3 min read
Control Statements in R Programming Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, we'll discuss all the control statements with the examples. In R pr
4 min read
Condition Handling in R Programming Decision handling or Condition handling is an important point in any programming language. Most of the use cases result in either positive or negative results. Sometimes there is the possibility of condition checking of more than one possibility and it lies with n number of possibilities. In this ar
5 min read
goto statement in R Programming Goto statement in a general programming sense is a command that takes the code to the specified line or block of code provided to it. This is helpful when the need is to jump from one programming section to the other without the use of functions and without creating an abnormal shift. Unfortunately,
2 min read
Difference Between & and && in R In R Programming Language, "&" and "&&" are two logical operators used to combine logical expressions. However, they behave differently in terms of how they evaluate expressions and return results. The "&" operator evaluates both expressions and returns a vector of the same length as
3 min read
And Operator In R The AND operator in R Programming Language is a logical operator used to combine multiple conditions or logical statements. It returns TRUE only if all combined conditions are true; otherwise, it returns FALSE. There are two types of AND operators in R Programming Language & and &&. This
3 min read