---
title: "Exercise 2.4 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 required the `tidyverse`, `janitor`, and `gt` packages. We will be exploring a data file, and introduce a few data manipulation options for data cleaning.

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

The goal of this exercise is to explore and consider how to clean a messy dataset. The Metropolitan Museum of Art in New York City maintains a database of more than 470,000 artworks. For the purposes of this exercise, we are going to focus on a small sample of objects in a file which requires some data cleaning.

This exercise is structured to encourage you to perform and explore each cleaning step separately and then combine them at the end. In practice, you are welcome to add each step as you go.

### (a) Read in the data set.

>The file is called `MetUnclean.csv` and you can read the file in using the `read_csv` function. Choose `met_unclean' as the name for the data frame if you want to be consistent with the rest of the exercise and the solutions we provide.

### (b) Have a look at the data file and see if you can identify any ways in which it might benefit from cleaning.

>One of the object titles doesn't seem quite right. What do you think might have happened?

### (c) Use `glimpse()` or the RStudio data view to see the names of the variables in the data file. One variable name includes a space. Write some code to remove this space, to make it easier to refer to that variable.

>Having spaces in a variable name requires the use of back ticks whenever that variable is referred to.  So a helpful step is to remove the space going forward. 
>
>We will use the `rename()` function to do this. You might like to replace the space with an underscore.
>
>Remember to save the result as a new data frame with a different name from the original data, e.g. `met_clean`.

### (d) Produce a frequency table of the Medium variable. Can you see the error? Write some code to correct it.

>Use the `replace` function within the `mutate` function to correct this. 
>
>Produce a frequency table using `tabyl` to check the results.

### (e) Produce a summary of the object age variable. Can you see that some missing data has been coded unhelpfully? Write some code to change these missing values into the R missing value code.

>Use the `summarise` function to look at the object age variables, as it is numeric. Use `na_if` within the `mutate` function to ensure the missing data point is stored in a more useful form.

### (f) Put all of your code together and create a new, clean data set with all the changes you have made.

> This should be one pipeline that starts with the `met_unclean` data frame and produces a clean data frame called `met_clean`.
>
> Remember, if you don't assign all your hard work to an object, it won't be saved anywhere.

### (g) Extension exercises
>
> Download the airport screening file used in lectures and attempt to perform your own cleaning exercise.

---

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