Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
What are vectors?
Vector operations
Indexing and filtering
15. Vector comparison
Summary

Instruction

Great! Let's talk about vector comparison. The comparison operators in R are:

  • less than (<),
  • less than or equal to (<=),
  • equal (==),
  • greater than (>),
  • greater than or equal to (>=),
  • not equal (!=).

You can use these operators to compare a vector with either a numerical value or another vector of the same size. Just as with the vector arithmetic operations we saw before, comparisons involving vectors are performed in a memberwise fashion. Thus, if you write 

ages < 35
R will compare each element of ages to the value 35. The comparison returns a vector that contains TRUE/ FALSE values. Such a vector is called a logical vector.

ages 28    29    43    39    31   29  ...
   < 35    35    35    35    35   35  ... 
-----------------------------------------
   TRUE  TRUE FALSE FALSE  TRUE TRUE  ...

You may be wondering why we'd ever want to make these comparisons. As we'll see in the next exercise, you can use a logical vector to perform vector filtering.

Exercise

Which employees are older than 35? Use a comparison operator and the vector ages to output a logical vector.