---
title: "Tutorial Week Lab 1 Question 5"
author: "Jennifer Chan"
date: "28/02/2020"
output: pdf_document
---

## Question 5

```{r setup, warning=F, message=F}
library(tidyverse)
```

Note that when loading the data, `rugby.txt` should be in the same directory as the R Markdown document. 

You can change the `html_document` to `pdf_document` for the **pdf** output but you need the latex installed. If it does not work for you then you should open your **html** output and print it to a pdf. Be sure you know how to do this before the assessment. Ask your lab tutor for help if you do not know how to get it done.

```{r data}
rugby <- read.table("rugby.txt", header = TRUE)
rugby[1:3, ]
head(rugby)
head(rugby, 2)
head(rugby$Game)
(tt <- table(rugby$Game))
```

Game `r names(tt)[tt==max(tt)]` had the most separate passages of play.

```{r barplot}
barplot(tt)
as.data.frame(tt) %>% # change to data frame first
  rename(Game=Var1, Count=Freq) %>% # rename the column names
  ggplot(aes(Game, Count)) + 
  geom_bar(stat="identity")
```

```{r hist}
hist(rugby$Time)
```

The histogram is right skewed and does not appear to be normally distributed.


```{r basehist}
timeA <- rugby %>% 
  filter(Game=="A") %>%
  pull(Time)
timeH <- rugby %>% 
  filter(Game=="H") %>%
  pull(Time)
hist(timeA)
hist(timeH)
```

```{r gghist}
rugby %>% 
  filter(Game %in% c("A", "H")) %>% 
  ggplot(aes(Time)) + 
  geom_histogram() + 
  facet_grid(Game ~ .)
```

You could alternatively code as follows (credit to Connor Smith) or any other way to achieve separate histograms.

```{r connorsolution, warning=F, message=F}
rugby_plot <- ggplot(rugby, aes(Time)) + geom_histogram()
rugby_plot %+% subset(x = rugby, Game == "A")
rugby_plot %+% subset(x = rugby, Game == "H")
```