Get Started

Greater and less than

Apart from equality operators, Filip also introduced the less than and greater than operators: < and >. You can also add an equal sign to express less than or equal to or greater than or equal to, respectively. Have a look at the following R expressions, that all evaluate to FALSE:

(1 + 2) > 4
"dog" < "Cats"
TRUE <= FALSE

Remember that for string comparison, R determines the greater than relationship based on alphabetical order. Also, keep in mind that TRUE is treated as 1 for arithmetic, and FALSE is treated as 0. Therefore, FALSE < TRUE is TRUE.

This is a part of the course

“Intermediate R”

View Course

Exercise instructions

Write R expressions to check whether:

  • -6 * 5 + 2 is greater than or equal to -10 + 1.
  • "raining" is less than or equal to "raining dogs".
  • TRUE is greater than FALSE.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Comparison of numerics


# Comparison of character strings


# Comparison of logicals

This exercise is part of the course

Intermediate R

BeginnerSkill Level
4.5+
131 reviews

Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

In this chapter, you'll learn about relational operators for comparing R objects, and logical operators like "and" and "or" for combining TRUE and FALSE values. Then, you'll use this knowledge to build conditional statements.

Exercise 1: Relational OperatorsExercise 2: EqualityExercise 3: Greater and less than
Exercise 4: Compare vectorsExercise 5: Compare matricesExercise 6: Logical OperatorsExercise 7: & and |Exercise 8: & and | (2)Exercise 9: Reverse the result: !Exercise 10: Blend it all togetherExercise 11: Conditional StatementsExercise 12: The if statementExercise 13: Add an elseExercise 14: Customize further: else ifExercise 15: Else if 2.0Exercise 16: Take control!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free