---
author: "Sean Laverty"
title: "Class 9 notes"
date: "`r Sys.Date()`"
format: html
editor: source
---

## Warning

Before you run this, be sure you have a directory called "csvs/" in this folder.  If you using this downloaded copy, you will.  But, in future tasks to repeat the final step you would need to ensure that the folder exists.  This is used to create the filenames (and paths) for the output `.csv` files.

## Layout

Using the `layout()` command, we can create nice layouts.  With `ggplot2` you can use the package `gridExtra` for comparable options. 

```{r}
#| label: fig-layout
#| fig.width: 5
#| fig.height: 5
#| fig.cap: "Random content in a nice plot layout."
layout(matrix(c(1, 1, 2, 1, 1, 2, 3, 3, 4), nrow = 3, ncol = 3, byrow = TRUE))
par(mar = c(4.1, 5.1, 0.8, 0.8))
plot(1:25, pch = 1:25, col = hcl.colors(25))
plot(runif(100, 0, 1))
plot(sin, xlim = c(0, 2*pi))
plot(cos, xlim = c(0, 2*pi))
```
## 2016 Rio Olympics data

Now we will read in the Olympics data.

```{r}
#| label: read-athlete-data
dat <- read.csv("./data/athletes.csv")
head(dat)
```

To calculate ages, we can append columns of the start date of the 2016 Rio Olympics and the participant's julian birthday. From those we can calculate the age in days as their difference.  Dividing that by 365^[overlooking leap years], we can approximate each participant's age in years.  You can probably do this a little more carefully with the `lubridate` package, since our current approach overlooks the existence of leap years.

```{r}
#| label: add-dates
dat$start <- julian(as.Date("2016-08-05", "%Y-%m-%d"))
head(dat$start)
dat$bday <- julian(as.Date(dat$date_of_birth, "%Y-%m-%d"))
head(dat$bday)
dat$age <- dat$start - dat$bday
dat$age <- dat$age/365
head(dat$age)
```

Just to check, it is nice to explore visualizations of the distribution of calculated ages.  If nothing else this gives us to chance to rule out unreasonable values.  For example, say we wrote but didn't execute the line above dividing the age in days by 365.

```{r}
#| label: fig-layout2
#| fig.width: 5
#| fig.height: 5
#| fig.cap: "Random content in a nice plot layout."
par(mfrow = c(2, 1), mar = c(4.1, 5.1, 0.8, 0.1))
hist(dat$age, las = 1)
boxplot(dat$age, horizontal = T, las = 1)
```
Now we can calculate, as an example of a reasonable task, the Olympians over 50 years old at the start of the games.  After making that data subset (which could have been done with something like `dat2 <- subset(dat, subset = age > 50))`.  After that we expore the corresponding nationalities and sports to make a table of their frequencies of co-occurring.

```{r}
#| label: older-olympians
dat2 <- dat[dat$age > 50, ]
dim(dat2)
head(dat2[ , c("nationality", "sport")])
dat3 <- as.data.frame(table(dat2[ , c("nationality", "sport")]))
head(dat3)
dat3 <- dat3[dat3$Freq > 0, ]
```

After keeping only the combinations of sports and nationalities that occur, we quickly write each to a text file.  There are a number of circumstances under which this exact task, or something quite structurally-similar, would be useful.  These particular output files are not very interesting, but this serves still as a good example of "dynamic filenames".  You could use the same "paste a variable value" technique for labeling graphs.

```{r}
#| label: write-file
dat3 <- dat3[order(dat3$Freq, decreasing = TRUE), ]
nms <- levels(dat3$nationality)
nms
for(n in nms) write.csv(dat3[dat3$nationality == n, ], paste("./csvs/", n, ".csv", sep = ""), row.names = FALSE)
```




