Fix broken links due to new base R chapter
This commit is contained in:
parent
3fed7013e7
commit
c9e1dad604
|
@ -1,4 +1,4 @@
|
|||
# A field guide to base R
|
||||
# A field guide to base R {#sec-base-r}
|
||||
|
||||
```{r}
|
||||
#| results: "asis"
|
||||
|
@ -31,7 +31,7 @@ To finish off, we'll briefly discuss two important plotting functions.
|
|||
library(tidyverse)
|
||||
```
|
||||
|
||||
## Selecting multiple elements with `[`
|
||||
## Selecting multiple elements with `[` {#sec-subset-many}
|
||||
|
||||
`[` is used to extract sub-components from vectors and data frames, and is called like `x[i]` or `x[i, j]`.
|
||||
In this section, we'll introduce you to the power of `[`, first showing you how you can use it with vectors, then how the same principles extend in a straightforward way to two-dimensional (2d) structures like data frames.
|
||||
|
@ -210,7 +210,7 @@ This function was the inspiration for much of dplyr's syntax.
|
|||
2. Why is `x[-which(x > 0)]` not the same as `x[x <= 0]`?
|
||||
Read the documentation for `which()` and do some experiments to figure it out.
|
||||
|
||||
## Selecting a single element `$` and `[[`
|
||||
## Selecting a single element `$` and `[[` {#sec-subset-one}
|
||||
|
||||
`[`, which selects many elements, is paired with `[[` and `$`, which extract a single element.
|
||||
In this section, we'll show you how to use `[[` and `$` to pull columns out of a data frames, discuss a couple more differences between `data.frames` and tibbles, and emphasize some important differences between `[` and `[[` when used with lists.
|
||||
|
|
|
@ -384,7 +384,7 @@ flights |>
|
|||
### Logical subsetting
|
||||
|
||||
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 `[` (pronounced subset) operator, which you'll learn more about in @sec-vector-subsetting.
|
||||
This makes use of the base `[` (pronounced subset) operator, which you'll learn more about in @sec-subset-many.
|
||||
|
||||
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:
|
||||
|
|
|
@ -691,7 +691,7 @@ Finally, don't forget what you learned in @sec-sample-size: whenever creating nu
|
|||
### Positions
|
||||
|
||||
There's one final type of summary that's useful for numeric vectors, but also works with every other type of value: extracting a value at specific position.
|
||||
You can do this with the base R `[` function, but we're not going to cover it until @sec-vector-subsetting, because it's a very powerful and general function.
|
||||
You can do this with the base R `[` function, but we're not going to cover it in detail until @sec-subset-many, because it's a very powerful and general function.
|
||||
For now we'll introduce three specialized functions that you can use to extract values at a specified position: `first(x)`, `last(x)`, and `nth(x, n)`.
|
||||
|
||||
For example, we can find the first and last departure for each day:
|
||||
|
|
11
program.qmd
11
program.qmd
|
@ -41,19 +41,20 @@ If you spend a little time rewriting your code while the ideas are fresh, you ca
|
|||
But this doesn't mean you should rewrite every function: you need to balance what you need to achieve now with saving time in the long run.
|
||||
(But the more you rewrite your functions the more likely your first attempt will be clear.)
|
||||
|
||||
In the following three chapters, you'll learn skills that will allow you to both tackle new programs and to solve existing problems with greater clarity and ease:
|
||||
In the following three chapters, you'll learn skills to improve your programming skills:
|
||||
|
||||
1. Copy-and-paste is a powerful tool, but you should avoid doing it more than twice.
|
||||
Repeating yourself in code is dangerous because it can easily lead to errors and inconsistencies.
|
||||
Instead, in @sec-functions, you'll learn how to write **functions** which let you extract out repeated code so that it can be easily reused.
|
||||
|
||||
2. As you start to write more powerful functions, you'll need a solid grounding in R's **data structures**, provided by vectors, which we discuss in @sec-vectors.
|
||||
You must master the four common atomic vectors, the three important S3 classes built on top of them, and understand the mysteries of the list and data frame.
|
||||
|
||||
3. Functions extract out repeated code, but you often need to repeat the same actions on different inputs.
|
||||
2. Functions extract out repeated code, but you often need to repeat the same actions on different inputs.
|
||||
You need tools for **iteration** that let you do similar things again and again.
|
||||
These tools include for loops and functional programming, which you'll learn about in @sec-iteration.
|
||||
|
||||
3. As you read more code written by others, you'll see more code that doesn't use the tidyverse.
|
||||
In @sec-base-r, you'll learn some of the most important base R functions that you'll see in the wild.
|
||||
These functions tend to be designed to use individual vectors, rather than data frames, often making them a good fit for your programming needs.
|
||||
|
||||
## Learning more
|
||||
|
||||
The goal of these chapters is to teach you the minimum about programming that you need to practice data science.
|
||||
|
|
|
@ -87,7 +87,7 @@ str(x5)
|
|||
```
|
||||
|
||||
As lists get even larger and more complex, `str()` eventually starts to fail, and you'll need to switch to `View()`[^rectangling-1].
|
||||
@fig-view-collapsed shows the result of calling `View(x4)`. The viewer starts by showing just the top level of the list, but you can interactively expand any of the components to see more, as in @fig-view-expand-1. RStudio will also show you the code you need to access that element, as in @fig-view-expand-2. We'll come back to how this code works in @sec-lists.
|
||||
@fig-view-collapsed shows the result of calling `View(x4)`. The viewer starts by showing just the top level of the list, but you can interactively expand any of the components to see more, as in @fig-view-expand-1. RStudio will also show you the code you need to access that element, as in @fig-view-expand-2. We'll come back to how this code works in @sec-subset-one.
|
||||
|
||||
[^rectangling-1]: This is an RStudio feature.
|
||||
|
||||
|
|
|
@ -115,7 +115,7 @@ But they're still good to know about even if you've never used `%>%` because you
|
|||
- The `|>` placeholder is deliberately simple and can't replicate many features of the `%>%` 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, `df %>% split(.$var)` is equivalent to `split(df, df$var)` and `df %>% {split(.$x, .$y)}` is equivalent to `split(df$x, df$y)`.
|
||||
|
||||
With `%>%` you can use `.` on the left-hand side of operators like `$`, `[[`, `[` (which you'll learn about in @sec-vectors), so you can extract a single column from a data frame with (e.g.) `mtcars %>% .$cyl`.
|
||||
With `%>%` you can use `.` on the left-hand side of operators like `$`, `[[`, `[` (which you'll learn about in @sec-subset-many), so you can extract a single column from a data frame with (e.g.) `mtcars %>% .$cyl`.
|
||||
A future version of R may add similar support for `|>` and `_`.
|
||||
For the special case of extracting a column out of a data frame, you can also use `dplyr::pull()`:
|
||||
|
||||
|
|
Loading…
Reference in New Issue