Minimise base-R a little
This commit is contained in:
parent
3c81995877
commit
4322a35d7c
66
base-R.qmd
66
base-R.qmd
|
@ -192,7 +192,10 @@ Base R also provides a function that combines the features of `filter()` and `se
|
|||
df |>
|
||||
filter(x > 1) |>
|
||||
select(y, z)
|
||||
```
|
||||
|
||||
```{r}
|
||||
#| results: false
|
||||
# same as
|
||||
df |> subset(x > 1, c(y, z))
|
||||
```
|
||||
|
@ -304,7 +307,9 @@ l <- list(
|
|||
|
||||
```{r}
|
||||
str(l[1:2])
|
||||
|
||||
str(l[1])
|
||||
|
||||
str(l[4])
|
||||
```
|
||||
|
||||
|
@ -315,67 +320,38 @@ l <- list(
|
|||
|
||||
```{r}
|
||||
str(l[[1]])
|
||||
|
||||
str(l[[4]])
|
||||
|
||||
str(l$a)
|
||||
```
|
||||
|
||||
The difference between `[` and `[[` is particularly important for lists because `[[` drills down into the list while `[` returns a new, smaller list.
|
||||
To help you remember the difference, take a look at the an unusual pepper shaker shown in @fig-pepper-1.
|
||||
If this pepper shaker is your list `pepper`, then, `pepper[1]` is a pepper shaker containing a single pepper packet, as in @fig-pepper-2.
|
||||
If we suppose this pepper shaker is a list `pepper`, then, `pepper[1]` is a pepper shaker containing a single pepper packet, as in @fig-pepper-2.
|
||||
To help you remember the difference, take a look at the an unusual pepper shaker shown in @fig-pepper.
|
||||
If this pepper shaker is your list `pepper`, then, `pepper[1]` is a pepper shaker containing a single pepper packet.
|
||||
If we suppose this pepper shaker is a list `pepper`, then, `pepper[1]` is a pepper shaker containing a single pepper packet.
|
||||
`pepper[2]` would look the same, but would contain the second packet.
|
||||
`pepper[1:2]` would be a pepper shaker containing two pepper packets.
|
||||
`pepper[[1]]` would extract the pepper packet itself, as in @fig-pepper-3.
|
||||
`pepper[[1]]` would extract the pepper packet itself.
|
||||
|
||||
```{r}
|
||||
#| label: fig-pepper-1
|
||||
#| label: fig-pepper
|
||||
#| echo: false
|
||||
#| out-width: "25%"
|
||||
#| out-width: "100%"
|
||||
#| fig-cap: >
|
||||
#| A pepper shaker that Hadley once found in his hotel room.
|
||||
#| (Left) A pepper shaker that Hadley once found in his hotel room.
|
||||
#| (Middle) `pepper[1]`.
|
||||
#| (Right) `pepper[[1]]`
|
||||
#| fig-alt: >
|
||||
#| A photo of a glass pepper shaker. Instead of the pepper shaker
|
||||
#| containing pepper, it contains many packets of pepper.
|
||||
#| Three photos. On the left is a photo of a glass pepper shaker. Instead of
|
||||
#| the pepper shaker containing pepper, it contain a single packet of pepper.
|
||||
#| In the middle is a photo of a single packet of pepper. On the right is a
|
||||
#| photo the contents of packet of pepper.
|
||||
|
||||
knitr::include_graphics("images/pepper.jpg")
|
||||
knitr::include_graphics("diagrams/pepper.png")
|
||||
```
|
||||
|
||||
```{r}
|
||||
#| label: fig-pepper-2
|
||||
#| echo: false
|
||||
#| out-width: "25%"
|
||||
#| fig-cap: >
|
||||
#| `pepper[1]`
|
||||
#| fig-alt: >
|
||||
#| A photo of the glass pepper shaker containing just one packet of
|
||||
#| pepper.
|
||||
|
||||
knitr::include_graphics("images/pepper-1.jpg")
|
||||
```
|
||||
|
||||
```{r}
|
||||
#| label: fig-pepper-3
|
||||
#| echo: false
|
||||
#| out-width: "25%"
|
||||
#| fig-cap: >
|
||||
#| `pepper[[1]]`
|
||||
#| fig-alt: A photo of single packet of pepper.
|
||||
|
||||
knitr::include_graphics("images/pepper-2.jpg")
|
||||
```
|
||||
|
||||
This same principle applies when you use 1d `[` with a data frame:
|
||||
|
||||
```{r}
|
||||
df <- tibble(x = 1:3, y = 3:5)
|
||||
|
||||
# returns a one-column data frame
|
||||
df["x"]
|
||||
|
||||
# returns the contents of x
|
||||
df[["x"]]
|
||||
```
|
||||
This same principle applies when you use 1d `[` with a data frame: `df["x"]` returns a one-column data frame and `df[["x"]]` returns a vector.
|
||||
|
||||
### Exercises
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 809 KiB |
Binary file not shown.
Before Width: | Height: | Size: 186 KiB |
Binary file not shown.
Before Width: | Height: | Size: 102 KiB |
Binary file not shown.
Before Width: | Height: | Size: 67 KiB |
Binary file not shown.
Before Width: | Height: | Size: 176 KiB |
Loading…
Reference in New Issue