How To Use Comparison Operators In R

Introduction

 
In this article I am going to demonstrate how to use comparison operators in R to filter the data of a particular dataset accordingly. To demonstrate the use of comparison operators we will be using dplyr package along with planes dataset in R. We will be using filter function provided with the 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 package named nycflights13. Therefore we need to load the package first as follows,
  1. library(nycflights13)  
Now we need to load the dataset planes as we will be using planes dataset to filter the data.
  1. > planes  
To use the filter function, we need to load the library named dplyr. We can use below syntax to load dplyr library,
  1. > planes  
  2. # A tibble: 3,322 x 9  
  3.    tailnum  year type                    manufacturer     model     engines seats speed engine     
  4.    <chr>   <int> <chr>                   <chr>            <chr>       <int> <int> <int> <chr>      
  5.  1 N10156   2004 Fixed wing multi engine EMBRAER          EMB-145XR       2    55    NA Turbo-fan  
  6.  2 N102UW   1998 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  7.  3 N103US   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  8.  4 N104UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  9.  5 N10575   2002 Fixed wing multi engine EMBRAER          EMB-145LR       2    55    NA Turbo-fan  
  10.  6 N105UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  11.  7 N107US   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  12.  8 N108UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  13.  9 N109UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  14. 10 N110UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  15. # ... with 3,312 more rows  
Using filter function
  1. > library(dplyr)  
  2. Attaching package: ‘dplyr’  
  3. The following objects are masked from ‘package:stats’:  
  4.     filter, lag  
  5. The following objects are masked from ‘package:base’:  
  6.     intersect, setdiff, setequal, union  
  7. >  
 

Different comparison 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. R contains various comparison operators such as >, >=, <, <=.
 
Greater than operator
 
Using greater than operator, we can display values which are greater than the value specified in the filter function.
 
Let us discuss this operator with the help of example below,
  1. > filter(planes, year > 1999, seats > 55)  
  2. # A tibble: 1,606 x 9  
  3.    tailnum  year type                    manufacturer     model    engines seats speed engine     
  4.    <chr>   <int> <chr>                   <chr>            <chr>      <int> <int> <int> <chr>      
  5.  1 N11206   2000 Fixed wing multi engine BOEING           737-824        2   149    NA Turbo-fan  
  6.  2 N117UW   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  7.  3 N118US   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  8.  4 N119US   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  9.  5 N121UW   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  10.  6 N122US   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  11.  7 N123UW   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  12.  8 N124US   2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214       2   182    NA Turbo-fan  
  13.  9 N125UW   2009 Fixed wing multi engine AIRBUS           A320-214       2   182    NA Turbo-fan  
  14. 10 N126UW   2009 Fixed wing multi engine AIRBUS           A320-214       2   182    NA Turbo-fan  
  15. # ... with 1,596 more rows  
In the argument part of the filter function, we have mentioned year > 1999 and seat > 55, therefore a new dataset containing all the observations of year greater than 1999 and all the observations of seat greater than 55 will be displayed.
 
Less than operator
 
Using less than operator, we can display values which are less than the value specified in the filter function.
 
Let us discuss this operator with the help of example below,
  1. > filter(planes, year < 1999, seats < 55)  
  2. # A tibble: 28 x 9  
  3.    tailnum  year type                     manufacturer         model       engines seats speed engine         
  4.    <chr>   <int> <chr>                    <chr>                <chr>         <int> <int> <int> <chr>          
  5.  1 N201AA   1959 Fixed wing single engine CESSNA               150               1     2    90 Reciprocating  
  6.  2 N202AA   1980 Fixed wing multi engine  CESSNA               421C              2     8    90 Reciprocating  
  7.  3 N344AA   1992 Fixed wing multi engine  GULFSTREAM AEROSPACE G-IV              2    22    NA Turbo-fan      
  8.  4 N347AA   1985 Rotorcraft               SIKORSKY             S-76A             2    14    NA Turbo-shaft    
  9.  5 N350AA   1980 Fixed wing multi engine  PIPER                PA-31-350         2     8   162 Reciprocating  
  10.  6 N364AA   1973 Fixed wing multi engine  CESSNA               310Q              2     6   167 Reciprocating  
  11.  7 N376AA   1978 Fixed wing single engine PIPER                PA-32RT-300       1     7    NA Reciprocating  
  12.  8 N378AA   1963 Fixed wing single engine CESSNA               172E              1     4   105 Reciprocating  
  13.  9 N383AA   1972 Fixed wing multi engine  BEECH                E-90              2    10    NA Turbo-prop     
  14. 10 N393AA   1994 Rotorcraft               BELL                 230               2    11    NA Turbo-shaft    
  15. # ... with 18 more rows  
In the argument part of the filter function, we have mentioned year < 1999 and seat < 55, therefore a new dataset containing all the observations of years less than 1999 and all the observations of seat less than 55 will be displayed.
 
Greater than or equal to operator
 
Using greater than or equal to operator, we can display values which are either greater than or equal to the value specified in the filter function.
 
Let us discuss this operator with the help of the example below,
  1. > filter(planes, year >= 1999, seats >= 55)  
  2. # A tibble: 2,148 x 9  
  3.    tailnum  year type                    manufacturer     model     engines seats speed engine     
  4.    <chr>   <int> <chr>                   <chr>            <chr>       <int> <int> <int> <chr>      
  5.  1 N10156   2004 Fixed wing multi engine EMBRAER          EMB-145XR       2    55    NA Turbo-fan  
  6.  2 N103US   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  7.  3 N104UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  8.  4 N10575   2002 Fixed wing multi engine EMBRAER          EMB-145LR       2    55    NA Turbo-fan  
  9.  5 N105UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  10.  6 N107US   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  11.  7 N108UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  12.  8 N109UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  13.  9 N110UW   1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214        2   182    NA Turbo-fan  
  14. 10 N11106   2002 Fixed wing multi engine EMBRAER          EMB-145XR       2    55    NA Turbo-fan  
  15. # ... with 2,138 more rows  
  16. >  
In the argument part of the filter function, we have mentioned year >= 1999 and seat >= 55, therefore a new dataset containing all the observations of year greater than or equal to 1999 and all the observations of seat greater than or equal to 55 will be displayed.
 
Less than or equal to operator
 
Using less than or equal to operator, we can display values which are either less than or equal to the value specified in the filter function.
 
Let us discuss this operator with the help of the example below,
  1. > filter(planes, year <= 1999, seats <= 55)  
  2. # A tibble: 76 x 9  
  3.    tailnum  year type                    manufacturer model     engines seats speed engine     
  4.    <chr>   <int> <chr>                   <chr>        <chr>       <int> <int> <int> <chr>      
  5.  1 N12957   1998 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  6.  2 N12967   1999 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  7.  3 N13949   1998 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  8.  4 N13955   1998 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  9.  5 N13956   1998 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  10.  6 N13958   1998 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  11.  7 N13964   1999 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  12.  8 N13965   1999 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  13.  9 N13968   1999 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  14. 10 N13969   1999 Fixed wing multi engine EMBRAER      EMB-145LR       2    55    NA Turbo-fan  
  15. # ... with 66 more rows  
In the argument part of the filter function, we have mentioned year <= 1999 and seat <= 55, therefore a new dataset containing all the observations of year less than or equal to 1999 and all the observations of seat less than or equal to 55 will be displayed.
 

Conclusion

 
In this article I demonstrated how to use comparison operators in R to filter the data of a particular dataset accordingly. To demonstrate the use of comparison operators, dplyr package along with planes dataset in R is used. Filter function which is provided with dplyr package to filter the data is also used.