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,
  1. library(nycflights13)
Now we need to load the dataset planes as we will be using planes dataset to filter the data,
  1. > planes
The above code will generate the following output,
  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
To use the filter function, we need to load the library named dplyr. We can use the below syntax to load dplyr library,
  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 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 !.
  1. > filter(planes, year == 2000 | year == 1998)
  2. # A tibble: 418 x 9
  3. tailnum year type manufacturer model engines seats speed engine
  4. <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
  5. 1 N102UW 1998 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
  6. 2 N11206 2000 Fixed wing multi engine BOEING 737-824 2 149 NA Turbo-fan
  7. 3 N117UW 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
  8. 4 N118US 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
  9. 5 N119US 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
  10. 6 N1200K 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
  11. 7 N1201P 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
  12. 8 N12125 1998 Fixed wing multi engine BOEING 757-224 2 178 NA Turbo-jet
  13. 9 N121UW 2000 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
  14. 10 N12216 1998 Fixed wing multi engine BOEING 737-824 2 149 NA Turbo-fan
  15. # ... with 408 more rows
The above code displays all planes belonging to 1998 or 2000.
  1. > filter(planes, year > 2004 | year < 1998)
  2. # A tibble: 1,790 x 9
  3. tailnum year type manufacturer model engines seats speed engine
  4. <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
  5. 1 N11181 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  6. 2 N11184 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  7. 3 N11187 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  8. 4 N11189 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  9. 5 N11191 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  10. 6 N11192 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  11. 7 N11193 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  12. 8 N11194 2005 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  13. 9 N11199 2006 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  14. 10 N12109 1994 Fixed wing multi engine BOEING 757-224 2 178 NA Turbo-jet
  15. # ... with 1,780 more rows
The above code displays all planes belonging to year greater than 2004 or less than 1998.
  1. > filter(planes, year %in% c(1998, 2004))
  2. # A tibble: 366 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 N11155 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  8. 4 N11164 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  9. 5 N11165 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  10. 6 N11176 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  11. 7 N1200K 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
  12. 8 N1201P 1998 Fixed wing multi engine BOEING 767-332 2 330 NA Turbo-fan
  13. 9 N12125 1998 Fixed wing multi engine BOEING 757-224 2 178 NA Turbo-jet
  14. 10 N12157 2004 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  15. # ... with 356 more rows
  16. >
The above code displays all planes belonging to 1998 or 2004.
  1. > filter(planes, !(seats > 55 | engines > 5))
  2. # A tibble: 512 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 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2 55 NA Turbo-fan
  7. 3 N11106 2002 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  8. 4 N11107 2002 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  9. 5 N11109 2002 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  10. 6 N11113 2002 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  11. 7 N11119 2002 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  12. 8 N11121 2003 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  13. 9 N11127 2003 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  14. 10 N11137 2003 Fixed wing multi engine EMBRAER EMB-145XR 2 55 NA Turbo-fan
  15. # ... with 502 more rows
The above code displays all planes having seats not greater than 55 or engines not greater than 5.
  1. > filter(planes, !(seats > 55 | engines > 1))
  2. # A tibble: 27 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 N315AT NA Fixed wing single engine JOHN G HESS AT-5 1 2 NA 4 Cycle
  7. 3 N376AA 1978 Fixed wing single engine PIPER PA-32RT-300 1 7 NA Reciprocating
  8. 4 N377AA NA Fixed wing single engine PAIR MIKE E FALCON XP 1 2 NA Reciprocating
  9. 5 N378AA 1963 Fixed wing single engine CESSNA 172E 1 4 105 Reciprocating
  10. 6 N394AA 2007 Fixed wing single engine AVIAT AIRCRAFT INC A-1B 1 2 NA Reciprocating
  11. 7 N397AA 1985 Fixed wing single engine STEWART MACO FALCON XP 1 2 NA Reciprocating
  12. 8 N425AA 1968 Fixed wing single engine PIPER PA-28-180 1 4 107 Reciprocating
  13. 9 N508AA 1975 Rotorcraft BELL 206B 1 5 112 Turbo-shaft
  14. 10 N508JB 2007 Fixed wing single engine CIRRUS DESIGN CORP SR22 1 4 NA Reciprocating
  15. # ... with 17 more rows
The above code displays all planes having seats not greater than 55 or engines not greater than 1.

Conclusion

In this article I demonstrated how to use Boolean logical operators in R to filter the data of a particular dataset accordingly. To demonstrate the use of logical operators, a 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.