Working with Vectors and Logical Subscripts

Take the example of a vector containing the 11 numbers 0 to 10:

x<-0:10

There are two quite different kinds of things we might want to do with this. We might want to add up the values of the elements:

sum(x)

[1] 55

Alternatively, we might want to count the elements that passed some logical criterion. Suppose we wanted to know how many of the values were less than 5:

sum(x<5)

[1] 5

You see the distinction. We use the vector function sum in both cases. But sum(x) adds up the values of the xs and sum(x<5) counts up the number of cases that pass the logical condition ‘x is less than 5’. This works because of coercion (p. 25). Logical TRUE has been coerced to numeric 1 and logical FALSE has been coerced to numeric 0.

That is all well and good, but how do you add up the values of just some of the elements of x? We specify a logical condition, but we don't want to count the number of cases that pass the condition, we want to add up all the values of the cases that pass. This is the final piece of the jigsaw, and involves the use of logical subscripts. Note that when we counted the number of cases, the counting was applied to the entire vector, using sum(x<5). To find the sum of the values of x that are less than 5, we write:

sum(x[x<5])

[1]  10

Let's look at this in more detail. The logical condition x<5 is either true or false:

x<5

 [1]   TRUE   TRUE  TRUE  TRUE  TRUE  FALSE  FALSE  FALSE  FALSE
[10]  FALSE  FALSE

You can imagine false as being ...

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.