Comparing Things

We can also do comparisons in R:

1 == 1  # equality (note two equals signs, read as "is equal to")

Output
[1] TRUE
1 != 2  # inequality (read as "is not equal to")

Output
[1] TRUE


1 < 2  # less than

Output
[1] TRUE


1 <= 1  # less than or equal to

Output
[1] TRUE


1 > 0  # greater than

Output
[1] TRUE


1 >= -9 # greater than or equal to

Output
[1] TRUE



Tip: Comparing Numbers

A word of warning about comparing numbers: you should never use == to compare two numbers unless they are integers (a data type which can specifically represent only whole numbers).

Computers may only represent decimal numbers with a certain degree of precision, so two numbers which look the same when printed out by R, may actually have different underlying representations and therefore be different by a small margin of error (called Machine numeric tolerance).

Instead you should use the all.equal function.