---
title: "Lab 2.1 - The MPG Dataset<sup>*</sup>"
author: "Author Goes Here"
date: "Semester Goes Here"
output:
  pdf_document:
    latex_engine: xelatex
---

In this document, we explore the MPG dataset provided in the TidyVerse package. It will follow the model of the lab 1.2 exploration of the Seattle Pets dataset.

## Loading the MPG Dataset

🚧
Start with a *code chunk* that loads the TidyVerse package and displays a summary of the structure and contents of the `mpg` dataset. Then include the following:

- a short description of the nature of this dataset
- a hyperlink to the MPG dataset reference page (`https://ggplot2.tidyverse.org/reference/mpg.html`). Be sure to display the hyperlink as shown here &mdash; PDF output doesn&rsquo;t support hyperlinks.
- You can suppress any ugly message output your code produces by turning off the messages and warnings in the code chunk header: `{r message=FALSE, warning=FALSE}`

## Reading and Writing Files

The MPG dataset is pre-loaded in the TidyVerse package, but we could save it to a file and reload it from there. 

🚧
To do this, include a code chunk that uses the *pipe* operator to pass the `mpg` dataset object into a call to the `write_csv()` *function*. You can find a specification of this function in Rstudio&rsquo; Data import cheat sheet (in RStudio, choose *Help*→*Search R Help* and search for &ldquo;write_csv&rdquo; &mdash; Look, in particular, at the examples at the end of the documentation.).

We can now read the MPG dataset back in from the file.

🚧
Include a code chunk that reads the CSV file you just wrote and assigned the input to a new object with an appropriate *name*. Display the structure of this re-read dataset and list the differences, if any, you see between it and the original MPG dataset.

## Including Images

RMarkdown allows us to include images.

🚧
Include RMarkdown code to include the RMarkdown logo found here
`https://rmarkdown.rstudio.com/docs/reference/figures/logo.png`

Note that the PDF conversion has issues with remote image files, but it works to download a local copy of the image. 

## Using Equations and Inline Code

RMarkdown also allows us to include equations and inline code computations. For example, Einstein's famous equation, $e = mc^2$, tells us that given the speed of light (29979245800 cm/second), the energy stored in a mass of 1 gram is: `r 1 * 29979245800**2`. (Yep, that's a lot of energy.)

🚧
Include a statement here that computes the mean city and highway MPG values. You can compute the mean using the `mean()` function and you can access the city and highway MPG value vectors using `mpg$cty` and `mpg$hwy`.

## Using the Intermediate Data Types

This document has already includes Tibbles (e.g., `mpg`), a particularly useful version of the standard data frame used to store datasets. The other data types we'll use in the course include Dates and Factors.

A *date* is a special type used to represent date-times. For example, it is currently `r lubridate::now()`. Dates can be manipulated using the `lubridate` package. 

🚧
Use the `lubridate::make_date(year = ??)` function to convert the MPG year variable into a date value. This will assume that the month and day are January 1.

A *factor* is a special type of *vector* used to represent categorical data values. For example, though the drive variable in the MPG dataset is represented as a character, it's probably best seen as a value from a short list of possible categories: 'f', '4', &hellip; 

🚧
Convert the MPG drive vector into a factor using the `as.factor()` function. What are the values for this variable in the dataset and what do they stand for?

## Exploring the Data

🚧
Include one query of the dataset here that shows something interesting. Include the R code, its output, and your interpretation of the results.

<hr>
<sup>*</sup>Exercise based on [Data Science in a Box](https://datasciencebox.org/)
