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:

> baskets.of.Granny <- c(12,4,4,6,9,3)
> baskets.of.Geraldine <- c(5,3,2,2,12,9)

Maybe you want to know the games in which Granny scored the fewest or the most baskets. For that purpose, R has a set of logical operators that — you guessed it — are nicely vectorized.

To illustrate, using the knowledge you have now, try to find out the games in which Granny scored the fewest baskets and the games in which she scored the most baskets:

  1. Create two logical vectors, as follows:

    > min.baskets <- baskets.of.Granny == min(baskets.of.Granny)
    > max.baskets <- baskets.of.Granny == max(baskets.of.Granny)

    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.

  2. 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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.