We don't want to create useless intermediate variables so we can use the pipe opperator: `%>%`
We don't want to create useless intermediate variables so we can use the pipe operator: `%>%`
(`ctrl + shift + M`).
```{r pipe_example_b, eval=F, message=F, cache=T}
...
...
@@ -303,7 +303,7 @@ flights %>%
Behind the scenes, `x %>% f(y)` turns into `f(x, y)`, and `x %>% f(y) %>% g(z)` turns into `g(f(x, y), z)` and so on. You can use the pipe to rewrite multiple operations in a way that you can read left-to-right, top-to-bottom.
<p><code>nycflights13::flights</code>contains all 336,776 flights that departed from New York City in 2013. The data comes from the US Bureau of Transportation Statistics, and is documented in <code>?flights</code></p>
<p><code>dplyr</code> functions never modify their inputs, so if you want to save the result, you’ll need to use the assignment operator, <code><-</code></p>
<divclass="sourceCode"id="cb5"><preclass="sourceCode r"><codeclass="sourceCode r"><spanid="cb5-1"><ahref="#cb5-1"></a>jan1 <-<spanclass="st"></span><spanclass="kw">filter</span>(flights, month <spanclass="op">==</span><spanclass="st"></span><spanclass="dv">1</span>, day <spanclass="op">==</span><spanclass="st"></span><spanclass="dv">1</span>)</span></code></pre></div>
<p>R either prints out the results, or saves them to a variable.</p>
<divclass="sourceCode"id="cb6"><preclass="sourceCode r"><codeclass="sourceCode r"><spanid="cb6-1"><ahref="#cb6-1"></a>(dec25 <-<spanclass="st"></span><spanclass="kw">filter</span>(flights, month <spanclass="op">==</span><spanclass="st"></span><spanclass="dv">12</span>, day <spanclass="op">==</span><spanclass="st"></span><spanclass="dv">25</span>))</span></code></pre></div>
<pre><code>## # A tibble: 719 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
<li><p>Brainstorm as many ways as possible to select <code>dep_time</code>, <code>dep_delay</code>, <code>arr_time</code>, and <code>arr_delay</code> from <code>flights</code>.</p></li>
<li><p>What does the <code>one_of()</code> function do? Why might it be helpful in conjunction with this vector?</p></li>
<li><p>Does the result of running the following code surprise you? How do the select helpers deal with case by default? How can you change that default?</p></li>
<li>Brainstorm as many ways as possible to select <code>dep_time</code>, <code>dep_delay</code>, <code>arr_time</code>, and <code>arr_delay</code> from <code>flights</code>.</li>
<li>What does the <code>one_of()</code> function do? Why might it be helpful in conjunction with this vector?</li>
<li>Does the result of running the following code surprise you? How do the select helpers deal with case by default? How can you change that default?</li>
<li>Currently <code>dep_time</code> and <code>sched_dep_time</code> are convenient to look at, but hard to compute with because they’re not really continuous numbers. Convert them to a more convenient representation of number of minutes since midnight.</li>
<li>Compare <code>dep_time</code>, <code>sched_dep_time</code>, and <code>dep_delay</code>. How would you expect those three numbers to be related?</li>
<li>Find the 10 most delayed flights using a ranking function. How do you want to handle ties? Carefully read the documentation for <code>min_rank()</code></li>
<h1>Combining multiple operations with the pipe</h1>
<p>Behind the scenes, <code>x %>% f(y)</code> turns into <code>f(x, y)</code>, and <code>x %>% f(y) %>% g(z)</code> turns into <code>g(f(x, y), z)</code> and so on. You can use the pipe to rewrite multiple operations in a way that you can read left-to-right, top-to-bottom.</p>
<p>You can access the transmited variables with <code>.</code></p>
<p>You can access the transmitted variables with <code>.</code></p>
<p>Working with the pipe is one of the key criteria for belonging to the <code>tidyverse</code>. The only exception is <code>ggplot2</code>: it was written before the pipe was discovered. Unfortunately, the next iteration of <code>ggplot2</code>, <code>ggvis</code>, which does use the pipe, isn’t quite ready for prime time yet.</p>