How to Summarize Logical Vectors in R

By Andrie de Vries, Joris Meys

If you use logical values in arithmetic operations, R sees TRUE as 1 and FALSE as 0. This allows for some pretty interesting constructs.

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)

Suppose that you want to know how often Granny scored more than Geraldine. You can use the numerical translation of a logical vector for that purpose in the sum() function, as follows:

> sum(
[1] 3

So, three times, Granny was better than Geraldine. Granny rocks!

In addition, you have an easy way to figure out whether any value in a logical vector is TRUE. Very conveniently, the function that performs that task is called any(). To ask R whether Granny was better than Geraldine in any game, use this code:

> any(
[1] TRUE

We told you that Granny rocks! Well, okay, this result is a bit unfair for Geraldine, so you should check whether Granny was better than Geraldine in all the games. The R function you use for this purpose is called — surprise, surprise — all(). To find out whether Granny was always better than Geraldine, use the following code:

> all(

Still, Granny rocks a bit.

You can use the argument na.rm=TRUE in the functions all() and any() as well. By default, both functions return NA if any value in the vector argument is missing.