0
0
R Programmingprogramming~5 mins

Comparison operators in R Programming

Choose your learning style9 modes available
Introduction

Comparison operators help you check if values are equal, bigger, smaller, or different. This lets your program make decisions.

Checking if a number is bigger than another to decide what to do next.
Finding out if two words are the same in a list.
Seeing if a value is not equal to a certain number before continuing.
Comparing dates to find which one comes first.
Filtering data to keep only items that meet a condition.
Syntax
R Programming
x == y  # equals
x != y  # not equals
x > y   # greater than
x < y   # less than
x >= y  # greater than or equal
x <= y  # less than or equal

Use double equals == to check if two values are the same.

Use != to check if two values are different.

Examples
Checks if 5 is equal to 5. Result is TRUE.
R Programming
5 == 5
Checks if "apple" is not equal to "orange". Result is TRUE.
R Programming
"apple" != "orange"
Checks if 10 is greater than 7. Result is TRUE.
R Programming
10 > 7
Checks if 3 is less than or equal to 2. Result is FALSE.
R Programming
3 <= 2
Sample Program

This program compares two numbers using different comparison operators and prints TRUE or FALSE for each check.

R Programming
a <- 8
b <- 10

print(a == b)   # Are a and b equal?
print(a != b)   # Are a and b different?
print(a > b)    # Is a greater than b?
print(a < b)    # Is a less than b?
print(a >= 8)   # Is a greater or equal to 8?
print(b <= 10)  # Is b less or equal to 10?
OutputSuccess
Important Notes

Comparison operators return TRUE or FALSE, which are logical values in R.

Use these operators inside if statements to control program flow.

Summary

Comparison operators compare two values and return TRUE or FALSE.

Use == for equality and != for inequality.

Other operators check if one value is bigger, smaller, or equal to another.