Skip to content

Commit

Permalink
Some fixes for chapters regexps & factors (#1636)
Browse files Browse the repository at this point in the history
* regexps.qmd: fix name of 'too_few' arg

* regexps.qmd: fix typo

* factors.qmd: update argument names to .f, .x, .y

* factors.qmd: fix language
  • Loading branch information
florisvdh committed Mar 2, 2024
1 parent 33e7012 commit f55997b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions factors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ It is hard to read this plot because there's no overall pattern.
We can improve it by reordering the levels of `relig` using `fct_reorder()`.
`fct_reorder()` takes three arguments:

- `f`, the factor whose levels you want to modify.
- `x`, a numeric vector that you want to use to reorder the levels.
- Optionally, `fun`, a function that's used if there are multiple values of `x` for each value of `f`. The default value is `median`.
- `.f`, the factor whose levels you want to modify.
- `.x`, a numeric vector that you want to use to reorder the levels.
- Optionally, `.fun`, a function that's used if there are multiple values of `.x` for each value of `.f`. The default value is `median`.

```{r}
#| fig-alt: |
Expand Down Expand Up @@ -231,7 +231,7 @@ Reserve `fct_reorder()` for factors whose levels are arbitrarily ordered.

However, it does make sense to pull "Not applicable" to the front with the other special levels.
You can use `fct_relevel()`.
It takes a factor, `f`, and then any number of levels that you want to move to the front of the line.
It takes a factor, `.f`, and then any number of levels that you want to move to the front of the line.

```{r}
#| fig-alt: |
Expand All @@ -247,7 +247,7 @@ ggplot(rincome_summary, aes(x = age, y = fct_relevel(rincome, "Not applicable"))
Why do you think the average age for "Not applicable" is so high?

Another type of reordering is useful when you are coloring the lines on a plot.
`fct_reorder2(f, x, y)` reorders the factor `f` by the `y` values associated with the largest `x` values.
`fct_reorder2(.f, .x, .y)` reorders the factor `.f` by the `.y` values associated with the largest `.x` values.
This makes the plot easier to read because the colors of the line at the far right of the plot will line up with the legend.

```{r}
Expand Down Expand Up @@ -287,7 +287,7 @@ Combine it with `fct_rev()` if you want them in increasing frequency so that in

```{r}
#| fig-alt: |
#| A bar char of marital status ordered in from least to most common:
#| A bar char of marital status ordered from least to most common:
#| no answer (~0), separated (~1,000), widowed (~2,000), divorced
#| (~3,000), never married (~5,000), married (~10,000).
gss_cat |>
Expand Down
4 changes: 2 additions & 2 deletions regexps.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ df |>
)
```

If the match fails, you can use `too_short = "debug"` to figure out what went wrong, just like `separate_wider_delim()` and `separate_wider_position()`.
If the match fails, you can use `too_few = "debug"` to figure out what went wrong, just like `separate_wider_delim()` and `separate_wider_position()`.

### Exercises

Expand Down Expand Up @@ -336,7 +336,7 @@ That lets you avoid one layer of escaping:
str_view(x, r"{\\}")
```

If you're trying to match a literal `.`, `$`, `|`, `*`, `+`, `?`, `{`, `}`, `(`, `)`, there's an alternative to using a backslash escape: you can use a character class: `[.]`, `[$]`, `[|]`, \...
If you're trying to match a literal `.`, `$`, `|`, `*`, `+`, `?`, `{`, `}`, `(`, `)`, there's an alternative to using a backslash escape: you can use a character class: `[.]`, `[$]`, `[|]`, ...
all match the literal values.

```{r}
Expand Down

0 comments on commit f55997b

Please sign in to comment.