---
title: "Exercise 3.3"
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)
```


### (a) Summarise the Datasaurus

> Load `Datasaurus.csv`. This file contains a few different datasets, which
> are indicated by the column `dataset`, each of which contain points with
> `x` and `y` coordinates.
>
> Use the `group_by()` and `summarise()` functions to calculate the means
> and standard deviations of the `x` and `y` variables, grouped by the
> `dataset` column. You could also include the correlation between `x` and
> `y` in your summary, using the function `cor(x, y)`.
>
> Then make a scatter plot of x vs y, faceted by the `dataset` column.

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


### (b) Convert the pig behaviour data to long form

> We've seen the data in `pig_behaviour_by_time.csv` in the lectures.
>
> Use `pivot_longer()` to convert the seven pig behaviours (in the columns
> `Upright` through `Nosing_pen`) into a factor `Behaviour` and measurement
> in variable `Number` (representing the number of pigs engaging in a particular
> behaviour).
>
> Use `mutate()` to derive a variable `Proportion`, representing the proportion
> of pigs engaging in a particular behaviour, by dividing by the `Total_pigs`
> variable.
>
> Store this data frame in a variable, and then display it using `glimpse()`.

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



### (c) Make a summary data frame from the pig behaviour data

> Use `group_by()` and `summarise()` to calculate the mean proportion of
> pigs engaging in each behaviour, for each level of `Housing` and `Treatment`.
> (You will need to use `na.rm = TRUE`, as there is missing data in
> this dataset.)
>
> Store this data frame in a variable, and then display it using `glimpse()`.



### (d) Convert the summary data frame into wide form

> Use `pivot_wider()` on the summary data frame to make one column for each
> treatment combination (i.e., each combination of Housing and Treatment).
>
> Hint: `pivot_wider()` allows you to provide more than one variable for
> `names_from`; e.g. `names_from = c(column1, column2)`.




---

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