---
title: "Exercise 4.1 solution"
author: "Put Your Name Here"
output:
  html_document:
    toc: true
---

<!-- You can knit this document at any time to see the draft of your answer. 
     An html file will be produced in the same folder as the Rmd file. -->

<!-- When you have completed the exercise, knit the document for the final time. -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  comment = NA,
  fig.width = 7,
  fig.height = 4,
  dpi = 130
)
```

In this exercise, we will be using functions from the `tidyverse` package.
You can see we've added the chunk option `message = FALSE` to hide the version
information that `tidyverse` normally displays.

```{r packages, message = FALSE}
library(tidyverse)
library(gt)
```

### (a) Make a summary table using gt

> We've provided some code to summarise the icecore data seen in previous
> exercises. Modify this to present the resulting data frame as a nice table,
> using the `gt()` function.
>
> Think about appropriate headings, column names, decimal places.

```{r, message = FALSE}
icecore <- read_csv("icecore.csv")
```

```{r}
icecore %>%
  group_by(core) %>%
  summarise(n_samples = n(),
            earliest_year_AD = min(air_age_AD),
            latest_year_AD = max(air_age_AD),
            min_depth_m = min(depth_m),
            max_depth_m = max(depth_m),
            mean_CO2_ppm = mean(CO2_ppm),
            sd_CO2_ppm = sd(CO2_ppm)) %>%
  ungroup() %>%
  gt(caption = md("CO<sub>2</sub> samples from a selection of Antarctic ice cores")) %>%
  fmt_number(n_samples:latest_year_AD, decimals = 0) %>%
  fmt_number(min_depth_m:sd_CO2_ppm, decimals = 1) %>%
  cols_label(
    core = "Core",
    n_samples = "Number of samples",
    earliest_year_AD = "Earliest",
    latest_year_AD = "Latest",
    min_depth_m = "Minimum",
    max_depth_m = "Maximum",
    mean_CO2_ppm = "Mean",
    sd_CO2_ppm = "SD"
  ) %>%
  tab_spanner("Year (AD)", earliest_year_AD:latest_year_AD) %>%
  tab_spanner("Depth (m)", min_depth_m:max_depth_m) %>%
  tab_spanner(md("CO<sub>2</sub> conc. (ppm)"), mean_CO2_ppm:sd_CO2_ppm)
```

If you were doing this in a real document, you might want to use the chunk
option `{r, echo = FALSE, message = FALSE}` so that the knitted document
displays the resulting table but not the code to produce it.


---

&copy; 2021 Statistical Consulting Centre, The University of Melbourne.
