Introduction to R and RStudio

Chapter 10 Logic

This lesson is meant to be a short introduction to logical operations in R.

There are two logical values in R, also called boolean values. They are TRUE and FALSE. In R you can construct logical expressions which will evaluate to either TRUE or FALSE.

Many of the questions in this lesson will involve evaluating logical expressions. It may be useful to open up a second R terminal where you can experiment with some of these expressions.

Creating logical expressions requires logical operators. You’re probably familiar with arithmetic operators like +, -, *, and /. The first logical operator we are going to discuss is the equality operator, represented by two equals signs ==. Use the equality operator below to find out if TRUE is equal to TRUE.

TRUE == TRUE
## [1] TRUE

Just like arithmetic, logical expressions can be grouped by parenthesis so that the entire expression (TRUE == TRUE) == TRUE evaluates to TRUE.

To test out this property, try evaluating (FALSE == TRUE) == FALSE .

(FALSE == TRUE) == FALSE
## [1] TRUE

The equality operator can also be used to compare numbers. Use == to see if 6 is equal to 7.

6 == 7
## [1] FALSE

The previous expression evaluates to FALSE because 6 is less than 7. Thankfully, there are inequality operators that allow us to test if a value is less than or greater than another value.

The less than operator < tests whether the number on the left side of the operator (called the left operand) is less than the number on the right side of the operator (called the right operand). Write an expression to test whether 6 is less than 7.

6 < 7
## [1] TRUE

There is also a less-than-or-equal-to operator <= which tests whether the left operand is less than or equal to the right operand. Write an expression to test whether 10 is less than or equal to 10.

10 <= 10
## [1] TRUE

Keep in mind that there are the corresponding greater than > and greater-than-or-equal-to >= operators.

Which of the following evaluates to FALSE?

  1. 9 >= 10
  2. 7 == 7
  3. 6 < 8
  4. 0 > -36

9 >= 10

The next operator we will discuss is the ‘not equals’ operator represented by !=. Not equals tests whether two values are unequal, so TRUE != FALSE evaluates to TRUE. Like the equality operator, != can also be used with numbers. Try writing an expression to see if 5 is not equal to 7.

5 != 7
## [1] TRUE

In order to negate boolean expressions you can use the NOT operator. An exclamation point ! will cause !TRUE (say: not true) to evaluate to FALSE and !FALSE (say: not false) to evaluate to TRUE. Try using the NOT operator and the equals operator to find the opposite of whether 5 is equal to 7.

!5 == 7
## [1] TRUE

Let’s take a moment to review. The equals operator == tests whether two boolean values or numbers are equal, the not equals operator != tests whether two boolean values or numbers are unequal, and the NOT operator ! negates logical expressions so that TRUE expressions become FALSE and FALSE expressions become TRUE.

At some point you may need to examine relationships between multiple logical expressions. This is where the AND operator and the OR operator come in.

Let’s look at how the AND operator works; &. The operator works as follows; if the right and left operands of AND are both TRUE then the entire expression is TRUE, and otherwise it is FALSE. For example, TRUE & TRUE evaluates to TRUE. Try typing FALSE & FALSE to how it is evaluated.

FALSE & FALSE
## [1] FALSE

You can use the & operator to evaluate AND across a vector. To test this, type the expression TRUE & c(TRUE, FALSE, FALSE).

TRUE & c(TRUE, FALSE, FALSE)
## [1]  TRUE FALSE FALSE

What happens in this case is that the left operand TRUE is recycled across every element in the vector of the right operand. This is the equivalent statement as c(TRUE, TRUE, TRUE) & c(TRUE, FALSE, FALSE).

The OR operator | follows a similar set of rules. An expression using the OR operator will evaluate to TRUE if the left operand or the right operand is TRUE. If both are TRUE, the expression will evaluate to TRUE, however if neither are TRUE, then the expression will be FALSE.

Let’s test out the vectorized version of the OR operator. Type the expression TRUE | c(TRUE, FALSE, FALSE).

TRUE | c(TRUE, FALSE, FALSE)
## [1] TRUE TRUE TRUE

For the next few questions, we’re going to need to create a vector of integers called ints. Create this vector by typing: ints <- sample(10)

ints <- sample(10)

Now simply display the contents of ints.

ints
##  [1]  2 10  8  1  6  5  7  4  3  9

The vector ints is a random sampling of integers from 1 to 10 without replacement. Let’s say we wanted to ask some logical questions about contents of ints. If we type ints > 5, we will get a logical vector corresponding to whether each element of ints is greater than 5. Try typing: ints > 5

ints > 5
##  [1] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE

We can use the resulting logical vector to ask other questions about ints. The which() function takes a logical vector as an argument and returns the indices of the vector that are TRUE. For example which(c(TRUE, FALSE, TRUE)) would return the vector c(1, 3).

Use the which() function to find the indices of ints that are greater than 7.

which(ints > 7)
## [1]  2  3 10

Which of the following commands would produce the indices of the elements in ints that are less than or equal to 2?

  1. ints < 2
  2. ints <= 2
  3. which(ints < 2)
  4. which(ints <= 2)

which(ints <= 2)

Like the which() function, the functions any() and all() take logical vectors as their argument. The any() function will return TRUE if one or more of the elements in the logical vector is TRUE. The all() function will return TRUE if every element in the logical vector is TRUE.

Use the any() function to see if any of the elements of ints are less than zero.

any(ints < 0)
## [1] FALSE

Use the all() function to see if all of the elements of ints are greater than zero.

all(ints > 0)
## [1] TRUE

Which of the following evaluates to TRUE?

  1. any(ints == 10)
  2. all(ints == 10)
  3. all(c(TRUE, FALSE, TRUE))
  4. any(ints == 2.5)

any(ints == 10)

You’ve successfully completed this lesson!