Kable Suggested Answers

Packages

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Comparing categorical variables: Penguins

Let’s make at table looking at the relationship between species and island.

To make the contingency table, we will use the function in dplry called pivot_wider(). It will take the data frame produced by count() that is current in a “long” format and reshape it to be in a “wide” format. We will also use the kable() function in the knitr package to neatly format our new table.

penguins |>
  count(species, island) |>
  pivot_wider(names_from = species,
              values_from = n,
              values_fill = 0) |>
  kable()
island Adelie Chinstrap Gentoo
Biscoe 44 0 124
Dream 56 68 0
Torgersen 52 0 0

Here is a great resource for kable here: https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html