Matching narrative to code, closes #1440

This commit is contained in:
mine-cetinkaya-rundel 2023-05-09 21:12:24 -04:00
parent adac82ba3f
commit 58547b1ec4
1 changed files with 6 additions and 5 deletions

View File

@ -3,6 +3,7 @@
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
status("complete")
```
@ -51,7 +52,7 @@ df |>
A very common way to create a logical vector is via a numeric comparison with `<`, `<=`, `>`, `>=`, `!=`, and `==`.
So far, we've mostly created logical variables transiently within `filter()` --- they are computed, used, and then thrown away.
For example, the following filter finds all daytime departures that leave roughly on time:
For example, the following filter finds all daytime departures that arrive roughly on time:
```{r}
flights |>
@ -332,7 +333,7 @@ There are two main logical summaries: `any()` and `all()`.
`all(x)` is equivalent of `&`; it'll return `TRUE` only if all values of `x` are `TRUE`'s.
Like all summary functions, they'll return `NA` if there are any missing values present, and as usual you can make the missing values go away with `na.rm = TRUE`.
For example, we could use `all()` and `any()` to find out if every flight was delayed by less than an hour or if any flights was delayed by over 5 hours.
For example, we could use `all()` and `any()` to find out if every flight was delayed on departure by at most an hour or if any flights were delayed on arrival by five hours or more.
And using `group_by()` allows us to do that by day:
```{r}
@ -353,7 +354,7 @@ That leads us to the numeric summaries.
When you use a logical vector in a numeric context, `TRUE` becomes 1 and `FALSE` becomes 0.
This makes `sum()` and `mean()` very useful with logical vectors because `sum(x)` gives the number of `TRUE`s and `mean(x)` gives the proportion of `TRUE`s (because `mean()` is just `sum()` divided by `length()`.
That, for example, allows us to see the proportion of flights that were delayed by less than 60 minutes and the number of flights that were delayed by over 5 hours:
That, for example, allows us to see the proportion of flights that were delayed on departure by at most an hour and the number of flights that were delayed on arrival by five hours or more:
```{r}
flights |>
@ -483,7 +484,7 @@ case_when(
This is more code, but it's also more explicit.
To explain how `case_when()` works, lets explore some simpler cases.
To explain how `case_when()` works, let's explore some simpler cases.
If none of the cases match, the output gets an `NA`:
```{r}
@ -560,7 +561,7 @@ We don't expect you to memorize these rules, but they should become second natur
### Exercises
1. A number is even if its divisible by two, which in R you can find out with `x %% 2 == 0`.
1. A number is even if it's divisible by two, which in R you can find out with `x %% 2 == 0`.
Use this fact and `if_else()` to determine whether each number between 0 and 20 is even or odd.
2. Given a vector of days like `x <- c("Monday", "Saturday", "Wednesday")`, use an `ifelse()` statement to label them as weekends or weekdays.