---
title: "rowing_submission_data_prep"
format: html
---

# Read in libraries

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

# Read in initial data set

Find the dataset on [this link](https://www.kaggle.com/datasets/heesoo37/120-years-of-olympic-history-athletes-and-results?resource=download) and download it. 

```{r}
athletes_df <- read_csv("athlete_events.csv")
```

# Set up rowing_df

```{r}
rowing_df <- athletes_df |> filter(Sport == "Rowing")
```

```{r}
rowing_df <- rowing_df |> filter(!is.na(Medal))
```

# Set up so that one athlete for each team represents the team

```{r}
country_event <- rowing_df |> 
  group_by(Year, Event, Medal, NOC) |> 
  select(Year, Event, NOC, Medal) |>
  slice(1) |> 
  arrange(Year, Event, Medal)
```

```{r}
country_medals <- country_event |> 
  mutate(n_point= case_when(
      Medal == "Gold" ~ 3, 
      Medal == "Silver" ~ 2, 
      Medal == "Bronze" ~ 1), 
    n_gold = case_when(
      Medal == "Gold" ~ 1, 
      Medal == "Silver" ~ 0, 
      Medal == "Bronze" ~ 0), 
    n_silver = case_when(
      Medal == "Gold" ~ 0, 
      Medal == "Silver" ~ 1,
      Medal == "Bronze" ~ 0),
    n_bronze = case_when(
      Medal == "Gold" ~ 0,
      Medal == "Silver" ~ 0,
      Medal == "Bronze" ~ 1)) |> 
  group_by(NOC) |>
  summarise(total_medals = n(), 
            total_points = sum(n_point),
            total_gold = sum(n_gold), 
            total_silver = sum(n_silver),
            total_bronze = sum(n_bronze))
```

```{r}
write_csv(country_medals, "rowing_medals.csv")
```
