Menu
Coddy logo textTech

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:

OperatorMeaningExampleResult
==Equal to5 == 5TRUE
!=Not equal to5 != 3TRUE
Greater than7 > 4TRUE
<Less than3 < 2FALSE
>=Greater than or equal to5 >= 5TRUE
<=Less than or equal to4 <= 3FALSE

You can compare variables just like you compare numbers directly:

age <- 25
is_adult <- age >= 18  # TRUE

Be 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 icon

Challenge

Easy

Evaluate 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 <- 72

Use comparison operators to create the following logical variables:

  1. Create a variable called passed that stores whether the student's score is greater than or equal to the passing score
  2. Create a variable called is_perfect that stores whether the student's score is equal to the perfect score
  3. Create a variable called above_average that stores whether the student's score is greater than the class average
  4. Create a variable called not_failing that stores whether the student's score is not equal to zero
  5. Create a variable called needs_improvement that 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.

OperatorMeaningExampleResult
==Equal to5 == 5TRUE
!=Not equal to5 != 3TRUE
Greater than7 > 4TRUE
<Less than3 < 2FALSE
>=Greater than or equal to5 >= 5TRUE
<=Less than or equal to4 <= 3FALSE

You can compare variables:

age <- 25
is_adult <- age >= 18  # TRUE

Note: 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals