How to Combine Logical Statements in R

Life would be boring in R if you couldn’t combine logical statements. If you want to test whether a number lies within a certain interval, for example, you want to check whether it’s greater than the lowest value and less than the top value.

To illustrate, let’s assume you have two vectors containing the number of baskets that Granny and her friend Geraldine scored in the six games of this basketball season:

min.baskets tells you whether the value is equal to the minimum, and max.baskets tells you whether the value is equal to the maximum.

Combine both vectors with the OR operator (|), as follows:

> min.baskets | max.baskets
[1] TRUE FALSE FALSE FALSE FALSE TRUE

This method actually isn’t the most efficient way to find those values. This example clearly shows you how vectorization works for logical operators.

The NOT operator (!) is another example of the great power of vectorization. The NA values in the vector x have caused some trouble already, so you’d probably like to get rid of them. You know that you have to check whether a value is missing by using the is.na() function.

But you need the values that are not missing values, so invert the logical vector by preceding it with the ! operator. To drop the missing values in the vector x, for example, use the following code:

> x[!is.na(x)]
[1] 3 6 2 1

When you’re using R, there’s no way to get around vectorization. After you understand how vectorization works, however, you’ll save considerable calculation time and lines of code.