Fix code language

This commit is contained in:
Hadley Wickham
2022-11-18 11:26:25 -06:00
parent 69b4597f3b
commit 868a35ca71
29 changed files with 912 additions and 907 deletions

View File

@@ -12,7 +12,7 @@
Why use a pipe?</h1>
<p>Each individual dplyr verb is quite simple, so solving complex problems typically requires combining multiple verbs. For example, the last chapter finished with a moderately complex pipe:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights |&gt;
<pre data-type="programlisting" data-code-language="r">flights |&gt;
filter(!is.na(arr_delay), !is.na(tailnum)) |&gt;
group_by(tailnum) |&gt;
summarise(
@@ -23,7 +23,7 @@ Why use a pipe?</h1>
<p>Even though this pipe has four steps, its easy to skim because the verbs come at the start of each line: start with the <code>flights</code> data, then filter, then group, then summarize.</p>
<p>What would happen if we didnt have the pipe? We could nest each function call inside the previous call:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">summarise(
<pre data-type="programlisting" data-code-language="r">summarise(
group_by(
filter(
flights,
@@ -38,7 +38,7 @@ Why use a pipe?</h1>
</div>
<p>Or we could use a bunch of intermediate variables:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">flights1 &lt;- filter(flights, !is.na(arr_delay), !is.na(tailnum))
<pre data-type="programlisting" data-code-language="r">flights1 &lt;- filter(flights, !is.na(arr_delay), !is.na(tailnum))
flights2 &lt;- group_by(flights1, tailnum)
flights3 &lt;- summarise(flight2,
delay = mean(arr_delay, na.rm = TRUE),
@@ -53,7 +53,7 @@ flights3 &lt;- summarise(flight2,
magrittr and the<code>%&gt;%</code> pipe</h1>
<p>If youve been using the tidyverse for a while, you might be familiar with the <code>%&gt;%</code> pipe provided by the <strong>magrittr</strong> package. The magrittr package is included in the core tidyverse, so you can use <code>%&gt;%</code> whenever you load the tidyverse:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
mtcars %&gt;%
group_by(cyl) %&gt;%
@@ -78,7 +78,7 @@ mtcars %&gt;%
<p>The <code>|&gt;</code> placeholder is deliberately simple and cant replicate many features of the <code>%&gt;%</code> placeholder: you cant pass it to multiple arguments, and it doesnt have any special behavior when the placeholder is used inside another function. For example, <code>df %&gt;% split(.$var)</code> is equivalent to <code>split(df, df$var)</code> and <code>df %&gt;% {split(.$x, .$y)}</code> is equivalent to <code>split(df$x, df$y)</code>.</p>
<p>With <code>%&gt;%</code> you can use <code>.</code> on the left-hand side of operators like <code>$</code>, <code>[[</code>, <code>[</code> (which youll learn about in <a href="#sec-subset-many" data-type="xref">#sec-subset-many</a>), so you can extract a single column from a data frame with (e.g.) <code>mtcars %&gt;% .$cyl</code>. A future version of R may add similar support for <code>|&gt;</code> and <code>_</code>. For the special case of extracting a column out of a data frame, you can also use <code><a href="https://dplyr.tidyverse.org/reference/pull.html">dplyr::pull()</a></code>:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="downlit">mtcars |&gt; pull(cyl)
<pre data-type="programlisting" data-code-language="r">mtcars |&gt; pull(cyl)
#&gt; [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4</pre>
</div>
</li>