Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Comparison Operators


Share

Comparison Operators

Comparison operators are used to compare two values. This helps us to find answers and make decisions in our programs.

The result of a comparison is either TRUE or FALSE. These are the logical (Boolean) values you learned about in the Booleans chapter.

In the following example, we use the greater than operator (>) to find out if x is greater than y:

Example

x <- 5
y <- 3

x > y
Try it Yourself »

A list of the comparison operators:

Operator Name Example Description Try it
== Equal to x == y Returns TRUE if the values are equal Try it »
!= Not equal to x != y Returns TRUE if the values are not equal Try it »
> Greater than x > y Returns TRUE if the first value is greater than the second Try it »
< Less than x < y Returns TRUE if the first value is less than the second Try it »
>= Greater than or equal to x >= y Returns TRUE if the first value is greater than, or equal to, the second Try it »
<= Less than or equal to x <= y Returns TRUE if the first value is less than, or equal to, the second Try it »

Note: Use two equal signs (==) to compare values. One equal sign (=) stores a value in a variable, just like <-.


Compare Strings

You can also compare strings. R is case-sensitive, so "apple" and "Apple" are not equal:

Example

"apple" == "apple"
"apple" == "Apple"
"apple" != "banana"
Try it Yourself »


Real-Life Examples

Comparison operators are often used to check rules, such as if a person is old enough to vote:

Example

age <- 18

age >= 18
age < 18
Try it Yourself »

Another common use is to check if a password is long enough. Here, we use nchar() from the Strings chapter to count the characters:

Example

password <- "abc12"

nchar(password) >= 8
Try it Yourself »

The password only has 5 characters, so the result is FALSE.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.