Adjusts
f_transform_yy_in_yyyy <- function(x){
ifelse(as.numeric(str_sub(x, start = -2, end = -1)) > 20,
paste0(str_sub(x, start = 1, end = -3), "19", str_sub(x, start = -2, end = -1)),
paste0(str_sub(x, start = 1, end = -3), "20", str_sub(x, start = -2, end = -1))
)
}
athletes <- athletes_raw %>%
dplyr::mutate(id = as.character(id)) %>%
#IMC
dplyr::mutate(IMC = weight/(height^2)) %>%
dplyr::relocate(IMC, .after = weight) %>%
#Age
dplyr::mutate(dob = f_transform_yy_in_yyyy(dob)) %>%
dplyr::mutate(dob = lubridate::mdy(dob)) %>%
dplyr::mutate(age = 2016 - lubridate::year(dob)) %>%
dplyr::relocate(age, .after = dob) %>%
#Medals Total
dplyr::mutate(medal_total = gold + silver + bronze) %>%
dplyr::mutate(medal_total_nonZero = ifelse(medal_total == 0, NA, medal_total)) %>%
#Medals Rank
dplyr::mutate(medal_rank =
dplyr::case_when(
gold > 0 ~ "gold",
silver > 0 ~ "silver",
bronze > 0 ~ "bronze",
TRUE ~ "none"
)) %>%
dplyr::mutate(medal_rank = as.ordered(medal_rank)) %>%
dplyr::mutate(medal_rank = forcats::fct_relevel(medal_rank, c("none", "bronze", "silver", "gold"))) %>%
dplyr::glimpse()
## Rows: 11,538
## Columns: 16
## $ id <chr> "736041664", "532037425", "435962603", "521041435"…
## $ name <chr> "A Jesus Garcia", "A Lam Shin", "Aaron Brown", "Aa…
## $ nationality <chr> "ESP", "KOR", "CAN", "MDA", "NZL", "AUS", "USA", "…
## $ sex <chr> "male", "female", "male", "male", "male", "male", …
## $ dob <date> 1969-10-17, 1986-09-23, 1992-05-27, 1991-01-02, 1…
## $ age <dbl> 47, 30, 24, 25, 26, 26, 23, 25, 28, 25, 19, 20, 24…
## $ height <dbl> 1.72, 1.68, 1.98, 1.83, 1.81, 1.80, 2.05, 1.93, 1.…
## $ weight <dbl> 64, 56, 79, 80, 71, 67, 98, 100, 62, 54, 63, 66, N…
## $ IMC <dbl> 21.63332, 19.84127, 20.15100, 23.88844, 21.67211, …
## $ sport <chr> "athletics", "fencing", "athletics", "taekwondo", …
## $ gold <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ silver <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ bronze <dbl> 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ medal_total <dbl> 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,…
## $ medal_total_nonZero <dbl> NA, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, …
## $ medal_rank <ord> none, none, bronze, none, none, none, bronze, none…