Introduction
Key elements of the visualization process
Environment - the R Language
13. Logical values

Instruction

The third type of data in R is logical. It is generated mostly by conditions ( e.g. 2 > 1, or "two is greater than one"). There are only two logical values in R: TRUE (when a condition is met) and FALSE (when it's not).

The logical operators that generate these conditions are shown in the table below:

R Operators
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= NOT equal to
& And
| Or

The AND and OR operators can be used to join conditions. For example, say you wanted to find ages that were greater than 18 and less than or equal to 100. You'd write:

( age > 18 ) & ( age <= 100 )

This will return TRUE only when the number variable age is greater than 18 AND equal to or less than 100.

Remember that TRUE and FALSE are reserved for logical values. This means you cannot use "true" or "false" as variable names or indeed as any kind of object name.

Exercise

The small bridge above the dolphin pool can only hold a limited number of people. You want to know whether 24 people is above this limit. The limit is already saved in the variable limit; write some code to see if 24 people will be safe on the bridge. Use the proper logical operator to check whether 24 is less than or equal to limit.

When you're done, press the Run and Check Code button.

Stuck? Here's a hint!

You should write:

24 <= limit