Introduction
In this article, I am going to demonstrate how to use the Dplyr package in R along with a plane dataset. We will be using various functions provided with the Dplyr package to manipulate and transform the data and to create a subset of data as well. The functions that we will be using are filter(), arrange(), and select().
Loading package and dataset
We will be using a predefined plane dataset that belongs to a package named nycflights13. Therefore we need to load the package first as follows.
# Loading the nycflights13 library
library(nycflights13)
Now we need to load the dataset planes as we will be using it to transform and manipulate the data.
planes
The above code will generate the following output.
# Displaying the 'planes' tibble with formatted columns
planes
# A tibble: 3,322 x 9
tailnum year type manufacturer model engines seats speed engine
<chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
2 N102UW 1998 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
3 N103US 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
4 N104UW 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
5 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
6 N105UW 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
7 N107US 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
8 N108UW 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
9 N109UW 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
10 N110UW 1999 Fixed wing multi engine AIRBUS INDUS... A320-214 2 182 NA Turbo-fan
# ... with 3,312 more rows
Now we need to load the library named Dplyr to use the functions of the package Dplyr. We can use the below syntax to load the Dplyr library.
# Loading the dplyr library
library(dplyr)
The above code will generate the following output.
library(dplyr)
# 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
Filter() function to filter data on the basis of variable names
The filter function is used to select and display observations according to the values of the arguments included inside the function. The filter function creates subsets of observations. Inside the function, the data frame name is the first argument and the second argument is the name of the variable of the dataset whose values we will be using to filter the data frame.
Let us discuss this function with the help of the example below.
# Filtering 'planes' tibble for year 1999
filter(planes, year == 1999)
# A tibble: 206 x 9
tailnum year type manufacturer model engines seats speed engine
<chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 N103US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
2 N104UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
3 N105UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
4 N107US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
5 N108UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
6 N109UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
7 N110UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
8 N111US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
9 N112US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
10 N113UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
# ... with 196 more rows
In the argument part of the filter function, we have mentioned year == 1999, therefore a new dataset containing all the observations of year 1999 will be displayed.
# Filtering 'planes' tibble for year 1999 and seats == 55
filter(planes, year == 1999, seats == 55)
# A tibble: 23 x 9
tailnum year type manufacturer model engines seats speed engine
<chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 N12967 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
2 N13964 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
3 N13965 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
4 N13968 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
5 N13969 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
6 N13970 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
7 N13975 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
8 N13978 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
9 N13979 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
10 N14972 1999 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
# ... with 13 more rows
In the argument part of the filter function, we have mentioned the value of the year as 1999 and the value of seats as 55, therefore filtering operation will be executed by Dplyr library and a new dataset containing all the observations of 1999 and containing 55 seats will be displayed.
Using assignment operator
The result of the new subset of the dataset that has been created can also be saved in another variable using the assignment operator.
# Filtering 'planes' tibble for year 2000 and seats == 55, assigning result to df
df <- filter(planes, year == 2000, seats == 55)
If we want to save the subset in a variable and print the result at the same time then we can include the entire assignment syntax inside parentheses as follows.
# Filtering 'planes' tibble for year 2000 and seats == 55, storing result in 'df'
(df <- filter(planes, year == 2000, seats == 55))
The above code will generate the following output.

Join the conversation! Your thoughts help the community grow.