library(tidyverse)
#start
url <- "https://raw.githubusercontent.com/Siganz/CUNY_Assignments/refs/heads/main/607/assignment_5/tournamentinfo.txt"
df_raw <- read_lines(url)
df_raw_t <- tibble(df_raw)
var_1 <- c("pair", "name", "total_pts", as.vector(rbind(paste0("dec_", 1:7), paste0("opp_", 1:7))))
var_2 <- c("state", "match", "pre_rating", "post_rating")
var_3 <- c("pair", "name", "state", "pre_rating", "post_rating", "total_pts", as.vector(rbind(paste0("dec_", 1:7), paste0("opp_", 1:7))))
var_4 <- c("name", "state", "pre_rating", "post_rating", "total_pts")
var_5 <- c(paste0("d_", 1:7))
#Working
df <- tidyr::tibble(raw = df_raw) |>
filter(
!str_detect(raw, "^-"),
!str_detect(raw, "\\s*Pair"),
!str_detect(raw, "\\s*Num")
) |>
mutate(index = row_number()) |>
mutate(
raw = str_trim(raw),
raw = str_replace(raw, "\\|$", ""),
raw = str_replace_all(raw, "\\|[BXN]\\s*", "|"),
raw = str_replace_all(raw, "\\s*\\|\\s*", "|"),
raw = str_replace_all(raw, "\\|:\\d\\s*", "|"),
raw = str_replace_all(raw, "P\\d+", ""),
raw = str_replace(raw, "->", "|"),
raw = str_replace(raw, "R:", ""),
raw = str_replace(raw, "/", "|"),
raw = str_replace(raw, "\\|*\\|$", ""),
raw = str_replace_all(raw, "\\|([WDL])\\s+(\\d+)", "|\\1|\\2"),
raw = str_replace_all(raw, "\\|([HU])\\s*(?=\\|)", "|\\1|0"),
raw = str_trim(raw)
)
df1 <- df |> filter(index %% 2 != 0) |>
separate(raw, sep = "\\|", into = var_1, extra = "drop", fill = "right") |>
mutate(across(everything(), str_trim)) |>
mutate(across(everything(), ~ na_if(.x, ""))) |>
mutate(across(everything(), ~ na_if(.x, "0")))
df2 <- df |> filter(index %% 2 == 0) |>
mutate(index = index - 1) |>
separate(raw, sep = "\\|", into = var_2, extra = "drop", fill = "right") |>
mutate(across(everything(), str_trim)) |>
mutate(across(everything(), ~ na_if(.x, "")))
player_df <- df1 |> left_join(df2, by ='index')|>
select(all_of(var_3)) |>
readr::type_convert()