diff --git a/base-R.qmd b/base-R.qmd index d93e098..fe28999 100644 --- a/base-R.qmd +++ b/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 diff --git a/diagrams/pepper.graffle b/diagrams/pepper.graffle new file mode 100644 index 0000000..154c70e Binary files /dev/null and b/diagrams/pepper.graffle differ diff --git a/diagrams/pepper.png b/diagrams/pepper.png new file mode 100644 index 0000000..05780ed Binary files /dev/null and b/diagrams/pepper.png differ diff --git a/images/pepper-1.jpg b/images/pepper-1.jpg deleted file mode 100644 index f95130a..0000000 Binary files a/images/pepper-1.jpg and /dev/null differ diff --git a/images/pepper-2.jpg b/images/pepper-2.jpg deleted file mode 100644 index cd69ace..0000000 Binary files a/images/pepper-2.jpg and /dev/null differ diff --git a/images/pepper-3.jpg b/images/pepper-3.jpg deleted file mode 100644 index e8c4a77..0000000 Binary files a/images/pepper-3.jpg and /dev/null differ diff --git a/images/pepper.jpg b/images/pepper.jpg deleted file mode 100644 index c974d3a..0000000 Binary files a/images/pepper.jpg and /dev/null differ