Introduction
In this article I am going to demonstrate how to use logical operators in R to filter the data of a particular dataset accordingly. To demonstrate the use of logical operators we will be using a dplyr package along with aplanes dataset in R. We will be using filter function provided with dplyr package to manipulate and transform the data and to create subset of data as well.
Loading package and dataset
We will be using predefined planes dataset which belongs to a package named nycflights13. Therefore we need to load the package first as follows,
- library(nycflights13)
Now we need to load the dataset planes as we will be using planes dataset to filter the data,
- > planes
The above code will generate the following output,
- > 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 INDUSTRIE A320-214 2 182 NA Turbo-fan
- 3 N103US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 4 N104UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE 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 INDUSTRIE A320-214 2 182 NA Turbo-fan
- 7 N107US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 8 N108UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 9 N109UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 10 N110UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- # ... with 3,312 more rows
To use the filter function, we need to load the library named dplyr. We can use the below syntax to load dplyr library,
- > 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
- >
Different logical operators
We can use filter function with different kinds of comparison operators to filter the dataset and to create a subset of data displaying data on the basis of conditions including comparison operators.
We can use Boolean logical operators to include multiple arguments in a filter function. These operators are and denoted by &, or denoted by | and not denoted by !.
- > filter(planes, year == 2000 | year == 1998)
- # A tibble: 418 x 9
- tailnum year type manufacturer model engines seats speed engine
- <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
- 1 N102UW 1998 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 2 N11206 2000 Fixed wing multi engine BOEING 737-824 2 149 NA Turbo-fan
- 3 N117UW 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 4 N118US 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 5 N119US 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 6 N1200K 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
- 7 N1201P 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
- 8 N12125 1998 Fixed wing multi engine BOEING 757-224 2 178 NA Turbo-jet
- 9 N121UW 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
- 10 N12216 1998 Fixed wing multi engine BOEING 737-824 2 149 NA Turbo-fan
- # ... with 408 more rows

Join the conversation! Your thoughts help the community grow.