- Here, we grouped the data by Year and Sex to count the unique athletes and medalists, then divided them to calculate the medal rate.
medal_summary <- medal_data %>%
group_by(Year, Sex) %>%
summarise(
n_athletes = n_distinct(ID),
n_medalists = n_distinct(ID[!is.na(Medal)]),
.groups = "drop"
) %>%
mutate(medal_rate = n_medalists/n_athletes)
head(medal_summary)
## # A tibble: 6 × 5
## Year Sex n_athletes n_medalists medal_rate
## <int> <chr> <int> <int> <dbl>
## 1 1896 M 176 93 0.528
## 2 1900 F 23 8 0.348
## 3 1900 M 1201 461 0.384
## 4 1904 F 6 5 0.833
## 5 1904 M 644 352 0.547
## 6 1906 F 6 5 0.833