---
title: "Summarising data example code"
author: "Statistical Consulting Centre"
output: 
  html_document:
    toc: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


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

### Reading in the data
 
In this R Markdown file, the Excel file that is read in is called analytic_data.xlxs. The data frame is called EXAMPLE_DATA.  Replace these with the names of the files you wish to use.

```{r, message=FALSE}
EXAMPLE_DATA <- read_excel("analytic_data.xlsx")
EXAMPLE_DATA <- EXAMPLE_DATA %>% 
  mutate_if(is.character,as.factor)
```

In all of the code below, you will need to replace EXAMPLE_DATA with the name of your data frame.
You will need to use the appropriate variable names.

### One numerical variable

```{r}
EXAMPLE_DATA %>%
  summarise(Mean = mean(NUMERICAL_VARIABLE1),
            "Standard Deviation" = sd(NUMERICAL_VARIABLE1),
            n = n()) %>%
  gt() %>%
  fmt_number(c(Mean, "Standard Deviation"),
             decimals = 1) %>%
  tab_spanner(
    label = "Nice label for NUMERICAL_VARIABLE1",
    columns = c(1:3))
```


### One numerical variable and one categorical variable

Mean, SD and n

```{r}
EXAMPLE_DATA %>%
  group_by(CATEGORICAL_VARIABLE1) %>%
  summarise(Mean = mean(NUMERICAL_VARIABLE1),
            "Standard Deviation" = sd(NUMERICAL_VARIABLE1),
            n = n()) %>%
  gt() %>%
  fmt_number(c(Mean, "Standard Deviation"),
             decimals = 1)
```

Five number summary, including median and quartiles

```{r}
EXAMPLE_DATA %>%
  group_by(CATEGORICAL_VARIABLE1) %>%
  summarise(n = n(),
            Minimum = min(NUMERICAL_VARIABLE1),
            "First quartile" = quantile(NUMERICAL_VARIABLE1, 0.25),
            Median = median(NUMERICAL_VARIABLE1),
            "Third quartile" = quantile(NUMERICAL_VARIABLE1, 0.75),
            Maximum = max(NUMERICAL_VARIABLE1))%>%
  gt() %>%
  fmt_number(c(Minimum, "First quartile", Median, "Third quartile", Maximum),
             decimals = 1)
```


### One categorical variable

```{r}
EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE1) %>%
  gt() %>%
  cols_label(percent = "proportion")
```

There are additional `adorn` functions that can be used to improve the `gt` tables further.

```{r}
EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE1) %>%
  adorn_totals("row") %>%
  adorn_pct_formatting() %>%
  gt()
```


### two categorical variables


```{r}
EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE1, CATEGORICAL_VARIABLE2) %>%
  gt()
```

An improvement adds the label of the second categorical variable so it is clear what the table is representing.

```{r}
EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE1, CATEGORICAL_VARIABLE2) %>%
  gt() %>%
  tab_spanner(
    label = "CATEGORICAL_VARIABLE2",
    columns = c(N,Y))
```

You can then use `adorn` to add in percentages.

```{r}
EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE1, CATEGORICAL_VARIABLE2) %>%
  adorn_percentages("row") %>%
  adorn_pct_formatting(digits = 0) %>%
  adorn_ns(position = "front") %>%
  gt() %>%
  tab_spanner(
    label = "CATEGORICAL_VARIABLE2",
    columns = c(N,Y))

```


© Statistical Consulting Centre, University of Melbourne, 2023