---
title: "Exercise 1.2"
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.
Before you use an R package, you need to load it into your session by calling
the `library` function. It's a good idea to load all of the packages you need
in a single code chunk at the top of your Rmarkdown file, like this:

```{r packages}
library(tidyverse)
```

### (a) Read in the Olympic 100m sprint medal results

> The file `Olympic_100m_results.csv` contains information on all Olympic 100m 
> sprint medalists. Open it in Excel first to see what it looks like, then read
> it into R using the `read_csv` function. You'll need to give the data frame
> a name; choose `olympic_100m_data` if you want to be consistent with the rest
> of the exercise and the solutions we provide.
>
> Hints:
>
> - If you're using a Windows computer, you will need to close the file in Excel
> before you can open it in using other software, including R.
>
> - If you get error messages about the file not being found, double check you
> have typed the name correctly, and make sure your R working directory is set
> right. The RStudio menu option
> **Session > Set Working Directory > To Source File Location**
> is often useful.
>
> - If you get error messages about `read_csv` not being found, make sure you
> have loaded the `tidyverse` package by running the code chunks near the top
> of this file.
>
> - Make sure you are using the `read_csv` function and not the `read.csv` function as these will not provide the same output.
>

<!-- Put your code to do this on a new line in the shaded region between the ```
     code chunk delimeters. -->
```{r import-data}

```

### (b) Looking at a data frame using RStudio

> When you load the data file, you should see its name appear in the Environment
> tab in RStudio (normally in the top-right panel).
>
> How many observations and how many variables does this data frame have?
>
> What happens when you click the blue arrow to the left of the data frame?
> What do you think `chr`, `num` and `logi` mean?
>
> What happens when you click on the name of the data frame
> (e.g. `olympic_100m_data`)?
>
> What do you think the data in each column represents?

DELETE THIS AND WRITE YOUR ANSWER HERE

### (c) Looking at a data frame using code

> Type the name of the data frame (e.g. `olympic_100m_data`) into the R console
> on the bottom-left and press enter. What do you see?
>
> Type `glimpse(NAME_OF_DATA_FRAME)` into the R console (changing that to the
> name of the data frame you chose earlier). What do you see?
>
> Now add these statements to an R code chunk. Try running them from the R
> Markdown document (Ctrl-Enter/Command-Enter or green 'play' button on the
> right.) Finally, knit the document into an html file and look at the result.

DELETE THIS AND WRITE YOUR ANSWER HERE. You will need to insert a code chunk
for the final part of the exercise.

### (d) Looking at columns of numeric data

> R refers to an individual column within a data frame using the notation
> `DATA_FRAME_NAME$COLUMN_NAME`. For example, the column `Time` in the data
> frame `olympic_100m_data` would be written `olympic_100m_data$Time`.
>
> - What happens when you write a line of code with just the name of a column
> within a data frame, e.g. `olympic_100m_data$Time`
>
> - What happens if you misspell the name of the column? What about using
> uppercase and lowercase differently from the name in the data frame?
>
> - There are a number of functions built into R which operate on numeric
> vectors. Some useful ones include `mean`, `sd`, `min`, `max` and `median`.
> Try calling these on the `Time` column; e.g. `mean(olympic_100m_data$Time)`
>
> R can also do arithmetic calculations, like a pocket calculator or Excel
> formula. You can mix single numbers with vectors (columns of numbers) and R
> will usually do something sensible. For example, `2 * column_name` will return
> a vector where each element of the original column has been doubled, while
> `column1 + column2` will add corresponding elements of each column.
>
> - Calculate the speed of each runner in metres per second. (Hint: speed
> is `distance / time`, and this data frame only contains information about
> a 100m race.)
>
> - What is the fastest speed in metres per second? (Hint: use the `max`
> function.)

DELETE THIS AND WRITE YOUR ANSWER HERE. You will need to insert a code chunk
to include R code.


### (e) Looking at columns of categorical data

> The `Event` and `Medal` columns in this data frame are categorical.
>
> - How does R display these columns if you type in their name,
> e.g. `olympic_100m_data$Event`?
>
> - You can get a simple frequency table using the `table()` function.
> Try calling these on the `Event` and `Medal` columns,
> e.g. `table(olympic_100m_data$Event)`

DELETE THIS AND WRITE YOUR ANSWER HERE. You will need to insert a code chunk
to include R code.

### (f) Getting help within R

> There are a few ways to get help within R. Try these out:
>
> 1. Click on the name of a function in a code chunk, and press F1.
> 2. Type `?functionname` in the R console pane (bottom left - don't add this
>    to your R Markdown file). e.g. `?table` (don't type the `back ticks` either!)
> 3. Type `help("functionname")` in the R console. e.g. `help("mean")`
> 4. Type `help(package = "packagename")` in the R console to get help on an
>    R package, e.g. `help(package = "ggplot2")`
>
> The first three all do the same thing.
>
> What do you notice about the documentation which comes with R?

DELETE THIS AND WRITE YOUR ANSWER HERE.

### (g) Clearing your R session

> There are two common ways to restore your R session to a fresh start.
>
> 1. Clear all variables in your workspace: click the 'broom' icon in the
> Environment panel (top-right) or **Session > Clear Workspace** in the menu.
>
> 2. Restart your R session: **Session > Restart R** in the menu.
>
> Try both of these. What do you think the difference is? (Hint: try running
> the code chunk with `read_csv` after each of them.)

DELETE THIS AND WRITE YOUR ANSWER HERE.

---

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