Filter Function in R Programming

Instructor: Magdiel Ablan

Magdiel teaches simulation modeling and statistics. She has a PhD in Environmental Sciences

In this lesson, you will learn how to use the filter function of the dplyr package, which transforms a data table by extracting only the rows that satisfy a given condition.

'filter' in R programming

One of the most important tasks in data analysis is data transformation. We may want to arrange the values in a certain way, drop or add some variables, or select only a subset of interesting cases, to name just a few. One of these tasks in data analysis is to select a subset of cases in the data that satisfy a given logical condition. That is exactly what the filter function does. It selects or filters the rows of the data table that meet certain criteria creating a new data subset.

Filter is a function of the dplyr package so, in order to use it, you need to have installed and loaded the dplyr package. If you don't know or don't remember how to do that, we will go over the necessary steps.

Dplyr

Dplyr is a powerful package or library that performs common data manipulation tasks in R like sorting, summarizing, variable selection and creation, among others. To install it in R just run from the command line:

install.packages('dplyr')

You will see some messages letting you know that the package is being installed in your local library. After it is installed, you should see a message like this printed out in your console:


** ....
** testing if installed package can be loaded
* DONE (dplyr)

The downloaded source packages are in: ....

Next, you need to load the library into your session using:

library(dplyr)

You will see this message:

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

filter, lag

The following objects are masked from 'package:base':

intersect, setdiff, setequal, union 

R is letting you know that the name of the functions in dplyr is the same that the name of the functions in some of the stats and base packages. That is something to keep in mind if you ever need to use these functions instead of the ones in dplyr. For the time being, no need to worry about that. You are now ready to start using filter.

'mtcars' Dataset

R comes with many built-in datasets. One of these data sets in mtcars. You can learn what this data set is about by typing at the console:

?mtcars

The help page tells us that the data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973-74 models). It explains what each of the variables are and how are they codified. You can visualize the first few rows of the data set using:

mtcars

We would have a similar, but much longer list of cars like this now.

One more step before we use filter. As you can see, car models are specified as row names in this dataset. Row names don't carry on when doing transformations. To keep this information in all subsequent analysis, we are going to:

  1. Save row names in a new variable, model
  2. Add it as a new variable in an improved version of mtcars, mtcars_n
  3. Drop row names in mtcars_n to avoid repetition.

This is how we do it:

model <- rownames(mtcars)
mtcars_n <- cbind(model,mtcars)
rownames(mtcars_n) <- c()

Just to make sure, let's take a look at mtcars_n:

head(mtcars_n,4)

The information is the same. We just changed the way car models are stored to make it easier to manipulate.

Using 'filter'

You are planning to buy a new car so you will practice the process of selecting car models with the characteristics you want using mtcars. Since you have a long commute, one of the most important characteristics for you is fuel efficiency. You want a buy a car with an average miles per gallon (mpg) greater than 25. The average mpg for each model is stored in the variable mpg. Thus, you will be selecting car models in mtcar_n that satisfy the criteria: mpg > 25. These two elements are the necessary arguments for the filter function:

filter(mtcars_n,mpg>25)

This is what you should get:

f3

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support