Search⌘ K

Filter Rows

Explore how to use the filter() function in R's dplyr package to select rows based on specific criteria. Understand the use of comparison operators and logical connectors to refine data frames for analysis.

We'll cover the following...

The filter() function allows us to specify criteria about the values of a variable in our dataset and then filters out only the rows that match that criteria.

We begin by focusing only on flights from New York City to Portland, Oregon. The dest destination code (or airport code) for Portland, Oregon is "PDX". Run the following and look at the results to ensure that only flights heading to Portland are chosen here:

R
portland_flights <- flights %>% filter(dest == "PDX")
print(portland_flights)

We’ll note the order of the code. First, take the data frame flights and apply the filter() function to the data frame so that only the rows with dest equals to "PDX" are included. We test for equality using the double equal sign == and not a single equal sign =. In other words, the filter (dest = "PDX") will yield an error. This is a convention across many programming languages. This might take some time, but ...