Hello, my name is David Christensen, and in these lessons, you will learn how to manipulate data with filtering, summarizing, and merging. And you will also learn how to work with missing values. In this lesson, I will show you how to filter data. Let's start with some example data. For purposes of this demonstration, we'll use the mtcars data set, which is built into R. So there's no need to load anything special to follow along. To see the mtcars data set, you can just type mtcars into the console. This outputs the data set. So what is this data? Well, it's about 30 observations or so about vehicles that appeared in Motor Trend magazine in 1974. You can see the names of the vehicles as row names. Let's have a quick look at some of the columns. The columns include MPG or miles per gallon, CYL, which is the number of cylinders, engine displacement in cubic inches, horsepower, the vehicle weight, type of transmission, along with some other vehicle attributes. Now let's say we only want to focus on the first five rows of data and ignore everything else. For this, we can use the head function. That's head, then open parentheses, followed by the data we want to filter. In this case, mtcars, then a comma, and then n equals five, closing parentheses. I'll press control enter to run this line of code. And here we go. We have the first five lines of the data set. If we wanted a different count of rows, we could have entered a different n value instead. This could have been 10 or 15 or 100 or as many rows of the data that we wanted to see. What if we wanted to see the last five rows of the data set instead of the first five rows? Well, we could use the tail function. Tail will return the last rows to your specification. To get the last rows, you could type tail, parentheses, mtcars, then a comma, and then you guessed it, n equals five, followed by a closing parentheses. I'll press control and enter, and this will output the last five rows. Head and tail are handy. I use them all the time when I just want to see the first or the last rows of a data set. But for robust filtering, we will need to learn filtering by index or with logical vectors. Let's have a look at these. To introduce index filtering, let's say we want only the sixth through the 10th row from the mtcars data set. Head and tail functions won't work for that, but we could row and column filter for the data we want. To filter by index, we will first type mtcars, followed by a square bracket, and then inside the square bracket, we will type the index for what we want to filter by. Let's give this a quick try by typing six colon 10. Six colon 10 is the same thing as if we had entered six, seven, eight, nine, and 10. Let's run six colon 10 on its own line so you can see this in action. As you can see, we get six, seven, eight, nine, 10. Okay, so we'll put six colon 10 in the square bracket, and then we'll type a closing bracket and press control and enter. What did we get? In the output, we see all rows, but only the sixth through the 10th column, along with row labels. Well, that's not quite what we want. To get the rows instead of the columns, we'll need to add a comma after the 10 and then run the code. Let's go ahead and try this. Now we get the correct rows by index value. One little point I wanted to add, we can filter by column and index values at the same time. Column index values can be added after the comma. If we only want the first two columns of data and the sixth through the 10th row, we can add one colon two after the comma. Let's go ahead and run this code. Empty cars, square bracket, six colon 10, comma, one colon two, closing square bracket. Great. This returns two columns, the MPG column, along with the cylinder column for the sixth through the 10th row. Now that we understand the syntax of index filtering, we can start adding logical operators to determine the index values we want to use. Let's consider the following. Let's say we want to return rows of empty cars where miles per gallon is more than 25. We want to know which rows are for cars that get good gas mileage by 1974 standards. Here's what we can do. We can type the which function, and within which we can type empty cars, the dollar sign, and then MPG. So we have MPG column of empty cars, and we want to express which is greater than 25. Let's return this line of code. It's returned the row numbers we want, row 18, 19, 20, 26, 27, and 28. These are the index values we need. We could go ahead and place these values between our bracket to filter. For example, empty cars, square bracket, 18, comma, closing bracket will filter to show the first row that we want, but we don't need to do that. We can write our filter right between our square brackets. Let's go ahead and do that. So we have empty cars, square bracket, which, parentheses, empty cars, dollar sign, MPG, greater than 25, closing parentheses, comma, and then closing square bracket. We'll go ahead and press control and enter at the same time. And now we've successfully filtered our data to only show cars with miles per gallon more than 25. Now that we've looked at index filtering, let's look at how we can express the same thing, but with less code. Remember, the which statement returned the row numbers 18, 19, 20, 26, 27, and 28. Let's remove the which statement and just type empty cars, dollar sign, MPG, greater than 25, and press control and enter. In the output, we get a logical vector. We get values false, false, false, false, false, false, false, false, false, false, all the way until the 18th value of the list, which is true. The 18th, 19th, 20th, 26th, 27th, and 28th value all show as true. We can add this logical vector between our square brackets to filter. For fun, let's go ahead and do that. Here is that logical vector pasted between square brackets. And if we run this line of code, we will get the rows where miles per gallon is greater than 25. But instead of actually typing this long logical vector between our square brackets, we could just type empty cars, square bracket, empty cars, dollar sign, MPG, greater than 25, comma, closing square bracket, and run this line of code. As before, we've successfully filtered empty cars to only show rows where miles per gallon is greater than 25. One thing to keep in mind with logical vectors is that they can be recycled. By this, I mean that the vectors of trues and falses do not have to be the same length as the data they are filtering. You could, for example, type false, false, true to return every third row. Or false, true to return every other row. Or true to return all rows. Or false to return no rows. All of those are between the square brackets. This kind of filtering comes in really handy in situations such as when filtering out odd or even rows. But it could also be misapplied if you have a logical test that is a different length than the data you are filtering. In the next video, we will have a look at combining multiple filters together and then creating summaries on the filtered data. Thanks for watching.