How to Search for Individual Words in R
How to Create an Array in R
How to Create Vectors in R

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)
> baskets.of.Geraldine <- c(5,3,2,2,12,9)

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]
[1] 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]
[1] 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
[1] 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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Hunt for Bugs in R
How to Create Different Plot Types in R
How to Access Alternative Resources for Help with R
How to Use the apply() Function to Summarize Arrays in R
How to Add Titles and Axis Labels to a Plot in R
Advertisement

Inside Dummies.com