The Comparison operators in R Programming are mostly used either in If Conditions or Loops. R Relational operators are commonly used to check the relationship between two variables.
- If the relation is true then it will return Boolean True
- If the relation is false then it will return Boolean False.
Below table shows all the Relational Operators in R Programming with examples.
Comparison Operators in R | Usage | Description | Example |
---|---|---|---|
> | i > j | i is greater than j | 25 > 14 returns True |
< | i < j | i is less than j | 25 < 14 returns False |
>= | i >= j | i is greater than or equal to j | 25 >= 14 returns True |
<= | i <= j | i is less than or equal to j | 25 <= 14 return False |
== | i == j | i is equal to j | 25 == 14 returns False |
!= | i != j | i is not equal to j | 25 != 14 returns True |
Comparison Operators in R Programming Example
This example helps you to understand the Comparison Operators in R Programming language practically. For this example, We are using two variables a and b and their respective values are 15 and 12. We are going to use these two variables to perform various relational operations present in R Programming
# Example for Comparison Operators in R Programming a <- 15 b <- 12 print(paste("Output of 15 > 12 is : ", a > b)) print(paste("Output of 15 < 12 is : ", a < b)) print(paste("Output of 15 >= 12 is : ", a >= b)) print(paste("Output of 15 <= 12 is : ", a <= b)) print(paste("Output of 15 Equal to 12 is : ", a == b)) print(paste("Output of 15 Not Equal to 12 is : ", a != b))
OUTPUT
ANALYSIS
We assigned two integer values a, b and we assigned the values 15 and 12 using the below statement.
a <- 15 b <- 12
In the next lines, We checked these values against each and every comparison operator (relational operator) present in the R Programming language.
R Comparison Operators in IF Statement
This example helps you to understand, how to use Comparison operators inside the If statements. For this example, We are using two variables x and y and their values are 25 and 25. We are going to use these two variables inside the If condition along with one of the comparison operator present in R programming to check the condition.
# Example for Relational Operators in R Programming x <- 25 y <- 25 if(x == y) { print("x is Equal to y") } else { print("x is NOT Equal to y") }
OUTPUT
ANALYSIS
We assigned two integer values x, y and assigned the values 25 and 25 using the below statement.
x <- 25 y <- 25
If Condition
If x is equal to y then the first print statement will be executed
print("x is Equal to y")
If the first condition fails then the second print statement will be executed.
print("x is NOT Equal to y")
Here 25 is equal to 25 so, the First print statement is printed
Thank You for Visiting Our Blog.