Fix code language
This commit is contained in:
@@ -11,18 +11,18 @@ Introduction</h1>
|
||||
Prerequisites</h2>
|
||||
<p>Most of the functions you’ll learn about in this chapter are provided by base R, so we don’t need the tidyverse, but we’ll still load it so we can use <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code>, <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>, and friends to work with data frames. We’ll also continue to draw examples from the nycflights13 dataset.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)
|
||||
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
|
||||
library(nycflights13)</pre>
|
||||
</div>
|
||||
<p>However, as we start to cover more tools, there won’t always be a perfect real example. So we’ll start making up some dummy data with <code><a href="https://rdrr.io/r/base/c.html">c()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c(1, 2, 3, 5, 7, 11, 13)
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c(1, 2, 3, 5, 7, 11, 13)
|
||||
x * 2
|
||||
#> [1] 2 4 6 10 14 22 26</pre>
|
||||
</div>
|
||||
<p>This makes it easier to explain individual functions at the cost of making it harder to see how it might apply to your data problems. Just remember that any manipulation we do to a free-floating vector, you can do to a variable inside data frame with <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> and friends.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(x)
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tibble(x)
|
||||
df |>
|
||||
mutate(y = x * 2)
|
||||
#> # A tibble: 7 × 2
|
||||
@@ -44,7 +44,7 @@ df |>
|
||||
Comparisons</h1>
|
||||
<p>A very common way to create a logical vector is via a numeric comparison with <code><</code>, <code><=</code>, <code>></code>, <code>>=</code>, <code>!=</code>, and <code>==</code>. So far, we’ve mostly created logical variables transiently within <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> — they are computed, used, and then thrown away. For example, the following filter finds all daytime departures that leave roughly on time:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(dep_time > 600 & dep_time < 2000 & abs(arr_delay) < 20)
|
||||
#> # A tibble: 172,286 × 19
|
||||
#> year month day dep_time sched_…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
|
||||
@@ -62,7 +62,7 @@ Comparisons</h1>
|
||||
</div>
|
||||
<p>It’s useful to know that this is a shortcut and you can explicitly create the underlying logical variables with <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
mutate(
|
||||
daytime = dep_time > 600 & dep_time < 2000,
|
||||
approx_ontime = abs(arr_delay) < 20,
|
||||
@@ -82,7 +82,7 @@ Comparisons</h1>
|
||||
<p>This is particularly useful for more complicated logic because naming the intermediate steps makes it easier to both read your code and check that each step has been computed correctly.</p>
|
||||
<p>All up, the initial filter is equivalent to:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
mutate(
|
||||
daytime = dep_time > 600 & dep_time < 2000,
|
||||
approx_ontime = abs(arr_delay) < 20,
|
||||
@@ -95,24 +95,24 @@ Comparisons</h1>
|
||||
Floating point comparison</h2>
|
||||
<p>Beware of using <code>==</code> with numbers. For example, it looks like this vector contains the numbers 1 and 2:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c(1 / 49 * 49, sqrt(2) ^ 2)
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c(1 / 49 * 49, sqrt(2) ^ 2)
|
||||
x
|
||||
#> [1] 1 2</pre>
|
||||
</div>
|
||||
<p>But if you test them for equality, you get <code>FALSE</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x == c(1, 2)
|
||||
<pre data-type="programlisting" data-code-language="r">x == c(1, 2)
|
||||
#> [1] FALSE FALSE</pre>
|
||||
</div>
|
||||
<p>What’s going on? Computers store numbers with a fixed number of decimal places so there’s no way to exactly represent 1/49 or <code>sqrt(2)</code> and subsequent computations will be very slightly off. We can see the exact values by calling <code><a href="https://rdrr.io/r/base/print.html">print()</a></code> with the the <code>digits</code><span data-type="footnote">R normally calls print for you (i.e. <code>x</code> is a shortcut for <code>print(x)</code>), but calling it explicitly is useful if you want to provide other arguments.</span> argument:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">print(x, digits = 16)
|
||||
<pre data-type="programlisting" data-code-language="r">print(x, digits = 16)
|
||||
#> [1] 0.9999999999999999 2.0000000000000004</pre>
|
||||
</div>
|
||||
<p>You can see why R defaults to rounding these numbers; they really are very close to what you expect.</p>
|
||||
<p>Now that you’ve seen why <code>==</code> is failing, what can you do about it? One option is to use <code><a href="https://dplyr.tidyverse.org/reference/near.html">dplyr::near()</a></code> which ignores small differences:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">near(x, c(1, 2))
|
||||
<pre data-type="programlisting" data-code-language="r">near(x, c(1, 2))
|
||||
#> [1] TRUE TRUE</pre>
|
||||
</div>
|
||||
</section>
|
||||
@@ -122,19 +122,19 @@ x
|
||||
Missing values</h2>
|
||||
<p>Missing values represent the unknown so they are “contagious”: almost any operation involving an unknown value will also be unknown:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">NA > 5
|
||||
<pre data-type="programlisting" data-code-language="r">NA > 5
|
||||
#> [1] NA
|
||||
10 == NA
|
||||
#> [1] NA</pre>
|
||||
</div>
|
||||
<p>The most confusing result is this one:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">NA == NA
|
||||
<pre data-type="programlisting" data-code-language="r">NA == NA
|
||||
#> [1] NA</pre>
|
||||
</div>
|
||||
<p>It’s easiest to understand why this is true if we artificially supply a little more context:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit"># Let x be Mary's age. We don't know how old she is.
|
||||
<pre data-type="programlisting" data-code-language="r"># Let x be Mary's age. We don't know how old she is.
|
||||
x <- NA
|
||||
|
||||
# Let y be John's age. We don't know how old he is.
|
||||
@@ -147,7 +147,7 @@ x == y
|
||||
</div>
|
||||
<p>So if you want to find all flights with <code>dep_time</code> is missing, the following code doesn’t work because <code>dep_time == NA</code> will yield a <code>NA</code> for every single row, and <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> automatically drops missing values:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(dep_time == NA)
|
||||
#> # A tibble: 0 × 19
|
||||
#> # … with 19 variables: year <int>, month <int>, day <int>, dep_time <int>,
|
||||
@@ -165,7 +165,7 @@ x == y
|
||||
</h2>
|
||||
<p><code>is.na(x)</code> works with any type of vector and returns <code>TRUE</code> for missing values and <code>FALSE</code> for everything else:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">is.na(c(TRUE, NA, FALSE))
|
||||
<pre data-type="programlisting" data-code-language="r">is.na(c(TRUE, NA, FALSE))
|
||||
#> [1] FALSE TRUE FALSE
|
||||
is.na(c(1, NA, 3))
|
||||
#> [1] FALSE TRUE FALSE
|
||||
@@ -174,7 +174,7 @@ is.na(c("a", NA, "b"))
|
||||
</div>
|
||||
<p>We can use <code><a href="https://rdrr.io/r/base/NA.html">is.na()</a></code> to find all the rows with a missing <code>dep_time</code>:</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(dep_time))
|
||||
#> # A tibble: 8,255 × 19
|
||||
#> year month day dep_time sched_…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
|
||||
@@ -192,7 +192,7 @@ is.na(c("a", NA, "b"))
|
||||
</div>
|
||||
<p><code><a href="https://rdrr.io/r/base/NA.html">is.na()</a></code> can also be useful in <code><a href="https://dplyr.tidyverse.org/reference/arrange.html">arrange()</a></code>. <code><a href="https://dplyr.tidyverse.org/reference/arrange.html">arrange()</a></code> usually puts all the missing values at the end but you can override this default by first sorting by <code><a href="https://rdrr.io/r/base/NA.html">is.na()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(month == 1, day == 1) |>
|
||||
arrange(dep_time)
|
||||
#> # A tibble: 842 × 19
|
||||
@@ -256,7 +256,7 @@ Boolean algebra</h1>
|
||||
Missing values</h2>
|
||||
<p>The rules for missing values in Boolean algebra are a little tricky to explain because they seem inconsistent at first glance:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(x = c(TRUE, FALSE, NA))
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tibble(x = c(TRUE, FALSE, NA))
|
||||
|
||||
df |>
|
||||
mutate(
|
||||
@@ -278,12 +278,12 @@ df |>
|
||||
Order of operations</h2>
|
||||
<p>Note that the order of operations doesn’t work like English. Take the following code finds all flights that departed in November or December:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(month == 11 | month == 12)</pre>
|
||||
</div>
|
||||
<p>You might be tempted to write it like you’d say in English: “find all flights that departed in November or December”:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(month == 11 | 12)
|
||||
#> # A tibble: 336,776 × 19
|
||||
#> year month day dep_time sched_…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
|
||||
@@ -301,7 +301,7 @@ Order of operations</h2>
|
||||
</div>
|
||||
<p>This code doesn’t error but it also doesn’t seem to have worked. What’s going on? Here R first evaluates <code>month == 11</code> creating a logical vector, which we call <code>nov</code>. It computes <code>nov | 12</code>. When you use a number with a logical operator it converts everything apart from 0 to TRUE, so this is equivalent to <code>nov | TRUE</code> which will always be <code>TRUE</code>, so every row will be selected:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
mutate(
|
||||
nov = month == 11,
|
||||
final = nov | 12,
|
||||
@@ -326,26 +326,26 @@ Order of operations</h2>
|
||||
</h2>
|
||||
<p>An easy way to avoid the problem of getting your <code>==</code>s and <code>|</code>s in the right order is to use <code>%in%</code>. <code>x %in% y</code> returns a logical vector the same length as <code>x</code> that is <code>TRUE</code> whenever a value in <code>x</code> is anywhere in <code>y</code> .</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">1:12 %in% c(1, 5, 11)
|
||||
<pre data-type="programlisting" data-code-language="r">1:12 %in% c(1, 5, 11)
|
||||
#> [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
|
||||
letters[1:10] %in% c("a", "e", "i", "o", "u")
|
||||
#> [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE</pre>
|
||||
</div>
|
||||
<p>So to find all flights in November and December we could write:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(month %in% c(11, 12))</pre>
|
||||
</div>
|
||||
<p>Note that <code>%in%</code> obeys different rules for <code>NA</code> to <code>==</code>, as <code>NA %in% NA</code> is <code>TRUE</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">c(1, 2, NA) == NA
|
||||
<pre data-type="programlisting" data-code-language="r">c(1, 2, NA) == NA
|
||||
#> [1] NA NA NA
|
||||
c(1, 2, NA) %in% NA
|
||||
#> [1] FALSE FALSE TRUE</pre>
|
||||
</div>
|
||||
<p>This can make for a useful shortcut:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(dep_time %in% c(NA, 0800))
|
||||
#> # A tibble: 8,803 × 19
|
||||
#> year month day dep_time sched_…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
|
||||
@@ -383,7 +383,7 @@ Logical summaries</h2>
|
||||
<p>There are two main logical summaries: <code><a href="https://rdrr.io/r/base/any.html">any()</a></code> and <code><a href="https://rdrr.io/r/base/all.html">all()</a></code>. <code>any(x)</code> is the equivalent of <code>|</code>; it’ll return <code>TRUE</code> if there are any <code>TRUE</code>’s in <code>x</code>. <code>all(x)</code> is equivalent of <code>&</code>; it’ll return <code>TRUE</code> only if all values of <code>x</code> are <code>TRUE</code>’s. Like all summary functions, they’ll return <code>NA</code> if there are any missing values present, and as usual you can make the missing values go away with <code>na.rm = TRUE</code>.</p>
|
||||
<p>For example, we could use <code><a href="https://rdrr.io/r/base/all.html">all()</a></code> to find out if there were days where every flight was delayed:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
group_by(year, month, day) |>
|
||||
summarise(
|
||||
all_delayed = all(arr_delay >= 0, na.rm = TRUE),
|
||||
@@ -409,7 +409,7 @@ Logical summaries</h2>
|
||||
Numeric summaries of logical vectors</h2>
|
||||
<p>When you use a logical vector in a numeric context, <code>TRUE</code> becomes 1 and <code>FALSE</code> becomes 0. This makes <code><a href="https://rdrr.io/r/base/sum.html">sum()</a></code> and <code><a href="https://rdrr.io/r/base/mean.html">mean()</a></code> very useful with logical vectors because <code>sum(x)</code> will give the number of <code>TRUE</code>s and <code>mean(x)</code> the proportion of <code>TRUE</code>s. That lets us see the distribution of delays across the days of the year as shown in <a href="#fig-prop-delayed-dist" data-type="xref">#fig-prop-delayed-dist</a>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
group_by(year, month, day) |>
|
||||
summarise(
|
||||
prop_delayed = mean(arr_delay > 0, na.rm = TRUE),
|
||||
@@ -426,7 +426,7 @@ Numeric summaries of logical vectors</h2>
|
||||
</div>
|
||||
<p>Or we could ask how many flights left before 5am, which are often flights that were delayed from the previous day:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
group_by(year, month, day) |>
|
||||
summarise(
|
||||
n_early = sum(dep_time < 500, na.rm = TRUE),
|
||||
@@ -452,7 +452,7 @@ Logical subsetting</h2>
|
||||
<p>There’s one final use for logical vectors in summaries: you can use a logical vector to filter a single variable to a subset of interest. This makes use of the base <code>[</code> (pronounced subset) operator, which you’ll learn more about in <a href="#sec-subset-many" data-type="xref">#sec-subset-many</a>.</p>
|
||||
<p>Imagine we wanted to look at the average delay just for flights that were actually delayed. One way to do so would be to first filter the flights:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
filter(arr_delay > 0) |>
|
||||
group_by(year, month, day) |>
|
||||
summarise(
|
||||
@@ -474,7 +474,7 @@ Logical subsetting</h2>
|
||||
<p>This works, but what if we wanted to also compute the average delay for flights that arrived early? We’d need to perform a separate filter step, and then figure out how to combine the two data frames together<span data-type="footnote">We’ll cover this in <a href="#chp-joins" data-type="xref">#chp-joins</a>]</span>. Instead you could use <code>[</code> to perform an inline filtering: <code>arr_delay[arr_delay > 0]</code> will yield only the positive arrival delays.</p>
|
||||
<p>This leads to:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
group_by(year, month, day) |>
|
||||
summarise(
|
||||
behind = mean(arr_delay[arr_delay > 0], na.rm = TRUE),
|
||||
@@ -516,30 +516,30 @@ Conditional transformations</h1>
|
||||
<p>If you want to use one value when a condition is true and another value when it’s <code>FALSE</code>, you can use <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">dplyr::if_else()</a></code><span data-type="footnote">dplyr’s <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code> is very similar to base R’s <code><a href="https://rdrr.io/r/base/ifelse.html">ifelse()</a></code>. There are two main advantages of <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code>over <code><a href="https://rdrr.io/r/base/ifelse.html">ifelse()</a></code>: you can choose what should happen to missing values, and <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code> is much more likely to give you a meaningful error if you variables have incompatible types.</span>. You’ll always use the first three argument of <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code>. The first argument, <code>condition</code>, is a logical vector, the second, <code>true</code>, gives the output when the condition is true, and the third, <code>false</code>, gives the output if the condition is false.</p>
|
||||
<p>Let’s begin with a simple example of labeling a numeric vector as either “+ve” or “-ve”:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c(-3:3, NA)
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c(-3:3, NA)
|
||||
if_else(x > 0, "+ve", "-ve")
|
||||
#> [1] "-ve" "-ve" "-ve" "-ve" "+ve" "+ve" "+ve" NA</pre>
|
||||
</div>
|
||||
<p>There’s an optional fourth argument, <code>missing</code> which will be used if the input is <code>NA</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">if_else(x > 0, "+ve", "-ve", "???")
|
||||
<pre data-type="programlisting" data-code-language="r">if_else(x > 0, "+ve", "-ve", "???")
|
||||
#> [1] "-ve" "-ve" "-ve" "-ve" "+ve" "+ve" "+ve" "???"</pre>
|
||||
</div>
|
||||
<p>You can also use vectors for the the <code>true</code> and <code>false</code> arguments. For example, this allows us to create a minimal implementation of <code><a href="https://rdrr.io/r/base/MathFun.html">abs()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">if_else(x < 0, -x, x)
|
||||
<pre data-type="programlisting" data-code-language="r">if_else(x < 0, -x, x)
|
||||
#> [1] 3 2 1 0 1 2 3 NA</pre>
|
||||
</div>
|
||||
<p>So far all the arguments have used the same vectors, but you can of course mix and match. For example, you could implement a simple version of <code><a href="https://dplyr.tidyverse.org/reference/coalesce.html">coalesce()</a></code> like this:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x1 <- c(NA, 1, 2, NA)
|
||||
<pre data-type="programlisting" data-code-language="r">x1 <- c(NA, 1, 2, NA)
|
||||
y1 <- c(3, NA, 4, 6)
|
||||
if_else(is.na(x1), y1, x1)
|
||||
#> [1] 3 1 2 6</pre>
|
||||
</div>
|
||||
<p>You might have noticed a small infelicity in our labeling: zero is neither positive nor negative. We could resolve this by adding an additional <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">if_else(x == 0, "0", if_else(x < 0, "-ve", "+ve"), "???")
|
||||
<pre data-type="programlisting" data-code-language="r">if_else(x == 0, "0", if_else(x < 0, "-ve", "+ve"), "???")
|
||||
#> [1] "-ve" "-ve" "-ve" "0" "+ve" "+ve" "+ve" "???"</pre>
|
||||
</div>
|
||||
<p>This is already a little hard to read, and you can imagine it would only get harder if you have more conditions. Instead, you can switch to <code><a href="https://dplyr.tidyverse.org/reference/case_when.html">dplyr::case_when()</a></code>.</p>
|
||||
@@ -552,7 +552,7 @@ if_else(is.na(x1), y1, x1)
|
||||
<p>dplyr’s <code><a href="https://dplyr.tidyverse.org/reference/case_when.html">case_when()</a></code> is inspired by SQL’s <code>CASE</code> statement and provides a flexible way of performing different computations for different computations. It has a special syntax that unfortunately looks like nothing else you’ll use in the tidyverse. It takes pairs that look like <code>condition ~ output</code>. <code>condition</code> must be a logical vector; when it’s <code>TRUE</code>, <code>output</code> will be used.</p>
|
||||
<p>This means we could recreate our previous nested <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code> as follows:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">case_when(
|
||||
<pre data-type="programlisting" data-code-language="r">case_when(
|
||||
x == 0 ~ "0",
|
||||
x < 0 ~ "-ve",
|
||||
x > 0 ~ "+ve",
|
||||
@@ -563,7 +563,7 @@ if_else(is.na(x1), y1, x1)
|
||||
<p>This is more code, but it’s also more explicit.</p>
|
||||
<p>To explain how <code><a href="https://dplyr.tidyverse.org/reference/case_when.html">case_when()</a></code> works, lets explore some simpler cases. If none of the cases match, the output gets an <code>NA</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">case_when(
|
||||
<pre data-type="programlisting" data-code-language="r">case_when(
|
||||
x < 0 ~ "-ve",
|
||||
x > 0 ~ "+ve"
|
||||
)
|
||||
@@ -571,7 +571,7 @@ if_else(is.na(x1), y1, x1)
|
||||
</div>
|
||||
<p>If you want to create a “default”/catch all value, use <code>TRUE</code> on the left hand side:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">case_when(
|
||||
<pre data-type="programlisting" data-code-language="r">case_when(
|
||||
x < 0 ~ "-ve",
|
||||
x > 0 ~ "+ve",
|
||||
TRUE ~ "???"
|
||||
@@ -580,7 +580,7 @@ if_else(is.na(x1), y1, x1)
|
||||
</div>
|
||||
<p>And note that if multiple conditions match, only the first will be used:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">case_when(
|
||||
<pre data-type="programlisting" data-code-language="r">case_when(
|
||||
x > 0 ~ "+ve",
|
||||
x > 3 ~ "big"
|
||||
)
|
||||
@@ -588,7 +588,7 @@ if_else(is.na(x1), y1, x1)
|
||||
</div>
|
||||
<p>Just like with <code><a href="https://dplyr.tidyverse.org/reference/if_else.html">if_else()</a></code> you can use variables on both sides of the <code>~</code> and you can mix and match variables as needed for your problem. For example, we could use <code><a href="https://dplyr.tidyverse.org/reference/case_when.html">case_when()</a></code> to provide some human readable labels for the arrival delay:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">flights |>
|
||||
<pre data-type="programlisting" data-code-language="r">flights |>
|
||||
mutate(
|
||||
status = case_when(
|
||||
is.na(arr_delay) ~ "cancelled",
|
||||
|
||||
Reference in New Issue
Block a user