Fix code language
This commit is contained in:
@@ -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 |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(!is.na(arr_delay), !is.na(tailnum)) |>
|
||||
group_by(tailnum) |>
|
||||
summarise(
|
||||
@@ -23,7 +23,7 @@ Why use a pipe?</h1>
|
||||
<p>Even though this pipe has four steps, it’s 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 didn’t 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 <- filter(flights, !is.na(arr_delay), !is.na(tailnum))
|
||||
<pre data-type="programlisting" data-code-language="r">flights1 <- filter(flights, !is.na(arr_delay), !is.na(tailnum))
|
||||
flights2 <- group_by(flights1, tailnum)
|
||||
flights3 <- summarise(flight2,
|
||||
delay = mean(arr_delay, na.rm = TRUE),
|
||||
@@ -53,7 +53,7 @@ flights3 <- summarise(flight2,
|
||||
magrittr and the<code>%>%</code> pipe</h1>
|
||||
<p>If you’ve been using the tidyverse for a while, you might be familiar with the <code>%>%</code> pipe provided by the <strong>magrittr</strong> package. The magrittr package is included in the core tidyverse, so you can use <code>%>%</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 %>%
|
||||
group_by(cyl) %>%
|
||||
@@ -78,7 +78,7 @@ mtcars %>%
|
||||
<p>The <code>|></code> placeholder is deliberately simple and can’t replicate many features of the <code>%>%</code> placeholder: you can’t pass it to multiple arguments, and it doesn’t have any special behavior when the placeholder is used inside another function. For example, <code>df %>% split(.$var)</code> is equivalent to <code>split(df, df$var)</code> and <code>df %>% {split(.$x, .$y)}</code> is equivalent to <code>split(df$x, df$y)</code>.</p>
|
||||
<p>With <code>%>%</code> you can use <code>.</code> on the left-hand side of operators like <code>$</code>, <code>[[</code>, <code>[</code> (which you’ll 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 %>% .$cyl</code>. A future version of R may add similar support for <code>|></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 |> pull(cyl)
|
||||
<pre data-type="programlisting" data-code-language="r">mtcars |> pull(cyl)
|
||||
#> [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>
|
||||
|
||||
Reference in New Issue
Block a user