# Which boats had a lower price than cost? # Writing the logical index by hand (you'd never do this!). We will learn how to create them and how to name their components. In R, true values are designated with TRUE, and false values with FALSE. Table 7.1 contains some that I frequently use: Logical vectors aren’t just good for indexing, you can also use them to figure out which values in a vector satisfy some criteria. For example, the following logical vector returns TRUE for cases where the boat colors are black OR brown, AND where the price was less than 100: When using multiple criteria, make sure to use parentheses when appropriate. As you can see, the result is identical to our previous result. If you use a logical vector to index, R returns a vector with only the values for which the logical vector is TRUE. You can use these logical vectors very efficiently to select some values from a vector. It only lets values of the vector pass through for which the logical vector is TRUE. Here's what I mean. Logical Index Vector A new vector can be sliced from a given vector with a logical index vector, which has the same length as the original vector. R list can contain a string, a numeric variable, a vector, a matrix, an array, a function, and even another list. There are different ways to do this, but it is generally easiest to use two numbers in a double index, the first for the row number (s) and the second for the column number (s). Pretty much any time you want to answer a question like “How many of X are Y” or “What percent of X are Y”, you use sum() or mean() function with a logical vector as an argument. # Which boats were eithe black or brown, AND had a price less than 100. You could create logical vectors directly using c(). For example, consider the following vector s of length 5. Logical Based Indexing One very useful method that R provides is to access elements of a vector using a different, logical vector of the same length. For example: # Which boats had a higher price than cost? How to Use Logical Vectors as Indices in R, How to Create a Data Frame from Scratch in R, How to Add Titles and Axis Labels to a Plot…. ; the first values will be compared, then the second values, etc.). # What were the prices of boats older than 100? When used with the indexing notation the items within a vector that are NA can be easily removed: > a <- c ( 1 , 2 , 3 , 4 , NA ) > is.na ( a ) [1] FALSE FALSE FALSE FALSE TRUE > ! Unfortunately, things aren’t so easy when the data is in a matrix (a 2D vector) and you want to access its elements using two index vectors (i.e., one indexing the matrix’s rows, and the second indexing its columns). You get the same answer: # Which boats had prices greater than 200 OR less than 100? Good for R aliens and R pirates. Many (if not all) R functions will interpret TRUE values as 1 and FALSE values as 0. This allows us to easily answer questions like “How many values in a data vector are greater than 0?” or “What percentage of values are equal to 5?” by applying the sum() or mean() function to a logical vector. With over 20 years of experience, he provides consulting and training services in the use of R. Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent. # one value m [2,2] ## b ## 5 # another one m [1,3] ## c ## 3. For example, we can compare the boat.cost and boat.price vectors to see which boats sold for a higher price than their cost: Once you’ve created a logical vector using a comparison operator, you can use it to index any vector with the same length. Version info: Code for this page was tested in R version 3.0.2 (2013-09-25) On: 2013-11-19 With: lattice 0.20-24; foreign 0.8-57; knitr 1.5 1. Figure 7.3: Logical comparison operators in R. However, creating logical vectors using c() is tedious. This is a really powerful tool. To use the %in% function, just put it in between the original vector, and a new vector of possible values. One of the nice things about logical indexing is that it is very easy and natural to combine the results of different conditions to select items based on multiple criteria. It may seem that this NA is translated into TRUE, but that isn’t the case. If that was confusing, think about it this way: a logical vector, combined with the brackets [ ], acts as a filter for the vector it is indexing. In this R list tutorial, we will explore the lists in the R programming language. If that was confusing, think about it this way: a logical vector, combined with the brackets [ ], acts as a filter for the vector it is indexing. R has lots of special functions that take vectors as arguments, and return logical vectors based on multiple criteria. logInd = X < target. logInd = Columns 1 through 13 1 0 1 0 0 0 0 0 0 0 0 0 0 Columns 14 through 20 1 0 0 0 0 0 1. 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: Use a logical vector, the.best, to tell you the games in which Granny scored more than Geraldine did.