Comparison Operators
Part of the Fundamentals section of Coddy's R journey — lesson 16 of 78.
Comparison operators let you compare two values and determine the relationship between them. Unlike arithmetic operators that return numbers, comparison operators always return a logical value: either TRUE or FALSE.
R provides six comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | TRUE |
!= | Not equal to | 5 != 3 | TRUE |
| Greater than | 7 > 4 | TRUE |
< | Less than | 3 < 2 | FALSE |
>= | Greater than or equal to | 5 >= 5 | TRUE |
<= | Less than or equal to | 4 <= 3 | FALSE |
You can compare variables just like you compare numbers directly:
age <- 25
is_adult <- age >= 18 # TRUEBe careful not to confuse = (assignment) with == (comparison). Using a single equals sign when you mean to compare is a common mistake that can lead to unexpected results.
Challenge
EasyEvaluate different conditions about a student's exam performance using comparison operators.
You are provided with the following variables:
student_score <- 78
passing_score <- 60
perfect_score <- 100
class_average <- 72Use comparison operators to create the following logical variables:
- Create a variable called
passedthat stores whether the student's score is greater than or equal to the passing score - Create a variable called
is_perfectthat stores whether the student's score is equal to the perfect score - Create a variable called
above_averagethat stores whether the student's score is greater than the class average - Create a variable called
not_failingthat stores whether the student's score is not equal to zero - Create a variable called
needs_improvementthat stores whether the student's score is less than the passing score
Use the print() function to display passed, is_perfect, above_average, not_failing, and needs_improvement in that exact order.
Cheat sheet
Comparison operators compare two values and return either TRUE or FALSE.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | TRUE |
!= | Not equal to | 5 != 3 | TRUE |
| Greater than | 7 > 4 | TRUE |
< | Less than | 3 < 2 | FALSE |
>= | Greater than or equal to | 5 >= 5 | TRUE |
<= | Less than or equal to | 4 <= 3 | FALSE |
You can compare variables:
age <- 25
is_adult <- age >= 18 # TRUENote: Don't confuse = (assignment) with == (comparison).
Try it yourself
# Given variables
student_score <- 78
passing_score <- 60
perfect_score <- 100
class_average <- 72
# TODO: Write your code below
# Use comparison operators to create the following logical variables:
# 1. passed - check if student_score >= passing_score
# 2. is_perfect - check if student_score == perfect_score
# 3. above_average - check if student_score > class_average
# 4. not_failing - check if student_score != 0
# 5. needs_improvement - check if student_score < passing_score
# Print the results in order
print(passed)
print(is_perfect)
print(above_average)
print(not_failing)
print(needs_improvement)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values