 How to Use Logical Vectors as Indices in R - dummies

# How to Use Logical Vectors as Indices in R

The index function in R doesn’t take only numerical vectors as arguments; it also works with logical vectors. You can use these logical vectors very efficiently to select some values from a vector.

If you use a logical vector to index, R returns a vector with only the values for which the logical vector is TRUE. 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)

Use a logical vector, the.best, to tell you the games in which Granny scored more than Geraldine did. If you want to know how many baskets Granny scored in those games, you can use this code:

```> baskets.of.Granny[the.best]
 12 5 6```

This construct is often used to keep only values that fulfill a certain requirement. If you want to keep only the values larger than 2 in the vector x, you could do that with the following code:

```> x <- c(3, 6, 1, NA, 2)
> x[x > 2]
 3 6 NA```

Wait — what is that NA value doing there? Take a step back, and look at the result of x > 2:

```> x > 2
 TRUE TRUE FALSE  NA FALSE```

If you have a missing value in your vector, any comparison returns NA for that value.

It may seem that this NA is translated into TRUE, but that isn’t the case. If you give NA as a value for the index, R puts NA in that place as well. So, in this case, R keeps the first and second values of x, drops the third, adds one missing value, and drops the last value of x as well.