Combined

filter(
  flights,
  arr_time >= 2200,
  carrier == "UA",
  arr_delay > 120,
  dest != "HNL"
)
## # A tibble: 330 x 19
##     year month   day dep_… sche… dep_… arr_… sche… arr_… carr… flig… tail…
##    <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr>
##  1  2013     1     2  2131  1512 379    2340  1741   359 UA      488 N593…
##  2  2013     1     9  1906  1727  99.0  2246  2040   126 UA     1100 N162…
##  3  2013     1    13  2021  1730 171    2328  2023   185 UA     1637 N732…
##  4  2013     1    23  1947  1629 198    2247  2005   162 UA     1078 N142…
##  5  2013     1    24  2016  1727 169    2342  2038   184 UA      738 N464…
##  6  2013     1    25  2012  1900  72.0  2302  2040   142 UA      954 N471…
##  7  2013     1    27  2025  1810 135    2203  1951   132 UA     1437 N772…
##  8  2013     1    30  1923  1529 234    2216  1837   219 UA     1624 N732…
##  9  2013     1    30  2039  1730 189    2340  2023   197 UA     1637 N732…
## 10  2013     1    31  1809  1629 100    2220  2005   135 UA     1078 N122…
## # ... with 320 more rows, and 7 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Alternative variant:

filter(
  flights,
  arr_time >= 2200 &
    carrier == "UA" &
    arr_delay > 120 &
    dest != "HNL"
)
## # A tibble: 330 x 19
##     year month   day dep_… sche… dep_… arr_… sche… arr_… carr… flig… tail…
##    <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr>
##  1  2013     1     2  2131  1512 379    2340  1741   359 UA      488 N593…
##  2  2013     1     9  1906  1727  99.0  2246  2040   126 UA     1100 N162…
##  3  2013     1    13  2021  1730 171    2328  2023   185 UA     1637 N732…
##  4  2013     1    23  1947  1629 198    2247  2005   162 UA     1078 N142…
##  5  2013     1    24  2016  1727 169    2342  2038   184 UA      738 N464…
##  6  2013     1    25  2012  1900  72.0  2302  2040   142 UA      954 N471…
##  7  2013     1    27  2025  1810 135    2203  1951   132 UA     1437 N772…
##  8  2013     1    30  1923  1529 234    2216  1837   219 UA     1624 N732…
##  9  2013     1    30  2039  1730 189    2340  2023   197 UA     1637 N732…
## 10  2013     1    31  1809  1629 100    2220  2005   135 UA     1078 N122…
## # ... with 320 more rows, and 7 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Intermediate variables

Naming is hard!

late_flights <- filter(flights, arr_time >= 2200)
late_ua_flights <- filter(late_flights, carrier == "UA")
late_late_ua_flights <- filter(late_ua_flights, arr_delay > 120)
late_late_ua_flights_not_honolulu <- filter(late_late_ua_flights, dest != "HNL")
late_late_ua_flights_not_honolulu
## # A tibble: 330 x 19
##     year month   day dep_… sche… dep_… arr_… sche… arr_… carr… flig… tail…
##    <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr>
##  1  2013     1     2  2131  1512 379    2340  1741   359 UA      488 N593…
##  2  2013     1     9  1906  1727  99.0  2246  2040   126 UA     1100 N162…
##  3  2013     1    13  2021  1730 171    2328  2023   185 UA     1637 N732…
##  4  2013     1    23  1947  1629 198    2247  2005   162 UA     1078 N142…
##  5  2013     1    24  2016  1727 169    2342  2038   184 UA      738 N464…
##  6  2013     1    25  2012  1900  72.0  2302  2040   142 UA      954 N471…
##  7  2013     1    27  2025  1810 135    2203  1951   132 UA     1437 N772…
##  8  2013     1    30  1923  1529 234    2216  1837   219 UA     1624 N732…
##  9  2013     1    30  2039  1730 189    2340  2023   197 UA     1637 N732…
## 10  2013     1    31  1809  1629 100    2220  2005   135 UA     1078 N122…
## # ... with 320 more rows, and 7 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Nested expressions

Difficult to read.

filter(
  filter(
    filter(
      filter(
        flights,
        arr_time >= 2200
      ),
      carrier == "UA"
    ),
    arr_delay > 120
  ),
  dest != "HNL"
)
## # A tibble: 330 x 19
##     year month   day dep_… sche… dep_… arr_… sche… arr_… carr… flig… tail…
##    <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr>
##  1  2013     1     2  2131  1512 379    2340  1741   359 UA      488 N593…
##  2  2013     1     9  1906  1727  99.0  2246  2040   126 UA     1100 N162…
##  3  2013     1    13  2021  1730 171    2328  2023   185 UA     1637 N732…
##  4  2013     1    23  1947  1629 198    2247  2005   162 UA     1078 N142…
##  5  2013     1    24  2016  1727 169    2342  2038   184 UA      738 N464…
##  6  2013     1    25  2012  1900  72.0  2302  2040   142 UA      954 N471…
##  7  2013     1    27  2025  1810 135    2203  1951   132 UA     1437 N772…
##  8  2013     1    30  1923  1529 234    2216  1837   219 UA     1624 N732…
##  9  2013     1    30  2039  1730 189    2340  2023   197 UA     1637 N732…
## 10  2013     1    31  1809  1629 100    2220  2005   135 UA     1078 N122…
## # ... with 320 more rows, and 7 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Pipe

flights %>% 
  filter(arr_time >= 2200) %>% 
  filter(carrier == "UA") %>% 
  filter(arr_delay > 120) %>% 
  filter(dest != "HNL")
## # A tibble: 330 x 19
##     year month   day dep_… sche… dep_… arr_… sche… arr_… carr… flig… tail…
##    <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr> <int> <chr>
##  1  2013     1     2  2131  1512 379    2340  1741   359 UA      488 N593…
##  2  2013     1     9  1906  1727  99.0  2246  2040   126 UA     1100 N162…
##  3  2013     1    13  2021  1730 171    2328  2023   185 UA     1637 N732…
##  4  2013     1    23  1947  1629 198    2247  2005   162 UA     1078 N142…
##  5  2013     1    24  2016  1727 169    2342  2038   184 UA      738 N464…
##  6  2013     1    25  2012  1900  72.0  2302  2040   142 UA      954 N471…
##  7  2013     1    27  2025  1810 135    2203  1951   132 UA     1437 N772…
##  8  2013     1    30  1923  1529 234    2216  1837   219 UA     1624 N732…
##  9  2013     1    30  2039  1730 189    2340  2023   197 UA     1637 N732…
## 10  2013     1    31  1809  1629 100    2220  2005   135 UA     1078 N122…
## # ... with 320 more rows, and 7 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Two options for assigning the result of a pipe to a variable:

late_late_ua_flights_not_honolulu <-
  flights %>% 
  filter(arr_time >= 2200) %>% 
  filter(carrier == "UA") %>% 
  filter(arr_delay > 120) %>% 
  filter(dest != "HNL")
flights %>% 
  filter(arr_time >= 2200) %>% 
  filter(carrier == "UA") %>% 
  filter(arr_delay > 120) %>% 
  filter(dest != "HNL") ->
  late_late_ua_flights_not_honolulu

Copyright © 2017 Kirill Müller. Licensed under CC BY-NC 4.0.