Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
7. Comparison operators
Text values
Variables
Functions
Summary

Instruction

Great job! Now, what if we want to compare the result of a calculation with some value? In R, comparisons are performed with the following operators:

  • Greater than (>),
  • Greater than or equal to (>=),
  • Less than (<),
  • Less than or equal to (<=),
  • Equal to (==),
  • Not equal to (!=).

When a comparison is made, R will return either TRUE or FALSE, which are called logical values. For example, if we would like to know if John's BMI is less than 30:

87/(1.90*1.90) < 30
R will return one of the following two values:

  • TRUE if his weight in kilograms divided by his height (in meters squared) is less than 30.
  • FALSE if his weight in kilograms divided by his height (in meters squared) is not less than 30 (which is equivalent to it being greater than or equal to 30).

Exercise

If you know that Tanya is 1.72 m tall and weighs 65 kilograms, use R to check whether she would be classified as obese (BMI greater than 30).

The BMI formula is as follows:

weight/(meters*meters)