INSTALLING LIBRARIES

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringr)

READING THE FILE PATH

getwd()
## [1] "C:/Users/kingk/OneDrive/Documents/CUNY MASTERS PROGRAM-DATA SCIENCE/Data Acquistion & Management 607/Assignments/Project 1"
list.files()
## [1] "chess_tournament_summary.csv" "Code_Base.Rmd"               
## [3] "Project1.requirements.pdf"    "tournamentinfo.txt.txt"
normalizePath(file.choose(), winslash = "/")
## [1] "C:/Users/kingk/OneDrive/Documents/CUNY MASTERS PROGRAM-DATA SCIENCE/Data Acquistion & Management 607/Assignments/Project 1/tournamentinfo.txt.txt"
tournament_lines <- readLines("C:/Users/kingk/OneDrive/Documents/CUNY MASTERS PROGRAM-DATA SCIENCE/Data Acquistion & Management 607/Assignments/Project 1/tournamentinfo.txt.txt", warn = FALSE)

head(tournament_lines, 20)
##  [1] "-----------------------------------------------------------------------------------------" 
##  [2] " Pair | Player Name                     |Total|Round|Round|Round|Round|Round|Round|Round| "
##  [3] " Num  | USCF ID / Rtg (Pre->Post)       | Pts |  1  |  2  |  3  |  4  |  5  |  6  |  7  | "
##  [4] "-----------------------------------------------------------------------------------------" 
##  [5] "    1 | GARY HUA                        |6.0  |W  39|W  21|W  18|W  14|W   7|D  12|D   4|" 
##  [6] "   ON | 15445895 / R: 1794   ->1817     |N:2  |W    |B    |W    |B    |W    |B    |W    |" 
##  [7] "-----------------------------------------------------------------------------------------" 
##  [8] "    2 | DAKSHESH DARURI                 |6.0  |W  63|W  58|L   4|W  17|W  16|W  20|W   7|" 
##  [9] "   MI | 14598900 / R: 1553   ->1663     |N:2  |B    |W    |B    |W    |B    |W    |B    |" 
## [10] "-----------------------------------------------------------------------------------------" 
## [11] "    3 | ADITYA BAJAJ                    |6.0  |L   8|W  61|W  25|W  21|W  11|W  13|W  12|" 
## [12] "   MI | 14959604 / R: 1384   ->1640     |N:2  |W    |B    |W    |B    |W    |B    |W    |" 
## [13] "-----------------------------------------------------------------------------------------" 
## [14] "    4 | PATRICK H SCHILLING             |5.5  |W  23|D  28|W   2|W  26|D   5|W  19|D   1|" 
## [15] "   MI | 12616049 / R: 1716   ->1744     |N:2  |W    |B    |W    |B    |W    |B    |B    |" 
## [16] "-----------------------------------------------------------------------------------------" 
## [17] "    5 | HANSHI ZUO                      |5.5  |W  45|W  37|D  12|D  13|D   4|W  14|W  17|" 
## [18] "   MI | 14601533 / R: 1655   ->1690     |N:2  |B    |W    |B    |W    |B    |W    |B    |" 
## [19] "-----------------------------------------------------------------------------------------" 
## [20] "    6 | HANSEN SONG                     |5.0  |W  34|D  29|L  11|W  35|D  10|W  27|W  21|"

CREATE THE PLAYERS TABLE

player_line_numbers <- which(
  str_detect(tournament_lines, "^\\s*\\d+\\s*\\|")
)

split_line <- function(line) {
  str_trim(str_split(line, fixed("|"))[[1]])
}

players <- bind_rows(lapply(player_line_numbers, function(i) {
  player_fields <- split_line(tournament_lines[i])
  detail_fields <- split_line(tournament_lines[i + 1])

  rating_match <- str_match(detail_fields[2], "R:\\s*(\\d+)")

  data.frame(
    pair_num = as.integer(player_fields[1]),
    name = player_fields[2],
    points = as.numeric(player_fields[3]),
    state = detail_fields[1],
    pre_rating = as.integer(rating_match[1, 2])
  )
}))

head(players)
##   pair_num                name points state pre_rating
## 1        1            GARY HUA    6.0    ON       1794
## 2        2     DAKSHESH DARURI    6.0    MI       1553
## 3        3        ADITYA BAJAJ    6.0    MI       1384
## 4        4 PATRICK H SCHILLING    5.5    MI       1716
## 5        5          HANSHI ZUO    5.5    MI       1655
## 6        6         HANSEN SONG    5.0    OH       1686

CREATE THE GAMES TABLES

games_list <- list()

for (row_number in player_line_numbers) {
  player_fields <- split_line(tournament_lines[row_number])

  round_results <- player_fields[4:10]
  opponent_numbers <- str_extract(round_results, "\\d+")

  games_list[[length(games_list) + 1]] <- data.frame(
    player_pair = as.integer(player_fields[1]),
    round = 1:7,
    opponent_pair = as.integer(opponent_numbers)
  )
}

games <- bind_rows(games_list)

games <- games |>
  filter(!is.na(opponent_pair), opponent_pair > 0)

head(games)
##   player_pair round opponent_pair
## 1           1     1            39
## 2           1     2            21
## 3           1     3            18
## 4           1     4            14
## 5           1     5             7
## 6           1     6            12

FINDING OPPONENT RATINGS

games_with_ratings <- games |>
  left_join(
    players |>
      select(pair_num, opponent_pre_rating = pre_rating),
    by = c("opponent_pair" = "pair_num")
  )

head(games_with_ratings)
##   player_pair round opponent_pair opponent_pre_rating
## 1           1     1            39                1436
## 2           1     2            21                1563
## 3           1     3            18                1600
## 4           1     4            14                1610
## 5           1     5             7                1649
## 6           1     6            12                1663

CALCULATING THE AVERAGE OPPONENT RATING / CREATE REQUIRED CSV

opponent_averages <- games_with_ratings |>
  group_by(player_pair) |>
  summarise(
    average_opponent_rating = mean(opponent_pre_rating, na.rm = TRUE),
    .groups = "drop"
  )

final_results <- players |>
  left_join(
    opponent_averages,
    by = c("pair_num" = "player_pair")
  ) |>
  transmute(
    player_name = name,
    player_state = state,
    total_number_of_points = points,
    player_pre_rating = pre_rating,
    average_pre_chess_rating_of_opponents = average_opponent_rating
  )

write.csv(
  final_results,
  "chess_tournament_summary.csv",
  row.names = FALSE
)

head(final_results)
##           player_name player_state total_number_of_points player_pre_rating
## 1            GARY HUA           ON                    6.0              1794
## 2     DAKSHESH DARURI           MI                    6.0              1553
## 3        ADITYA BAJAJ           MI                    6.0              1384
## 4 PATRICK H SCHILLING           MI                    5.5              1716
## 5          HANSHI ZUO           MI                    5.5              1655
## 6         HANSEN SONG           OH                    5.0              1686
##   average_pre_chess_rating_of_opponents
## 1                              1605.286
## 2                              1469.286
## 3                              1563.571
## 4                              1573.571
## 5                              1500.857
## 6                              1518.714

CONCLUSION AND ANALYTICAL FINDINGS

The tournament data includes 64 players. Their total points ranged from 1.0 to 6.0, with an average of 3.44 points. Three players tied for the highest score of 6.0: Gary Hua, Dakshesh Daruri, and Aditya Bajaj. The average pre-tournament rating of each player’s opponents ranged from approximately 1106.6 to 1605.3. Gary Hua faced the highest-rated opposition on average, at approximately 1605.3, and scored 6.0 points. Aditya Bajaj also scored 6.0 points and faced opponents averaging approximately 1563.6. These values describe the opponents each player faced; they do not by themselves explain why a player earned a particular score.

Tournament points show how many points each player earned from their games. Average opponent rating adds context by describing the pre-tournament ratings of the players they faced. The three top scorers all earned 6.0 points, even though their opponents’ average ratings differed. This shows why it is useful to consider both measures together. The player who faced the highest average opponent rating did not necessarily earn more points than every other player. These are separate measures: one summarizes game results, while the other summarizes opponents’ pre-tournament ratings. The analysis is descriptive and does not establish that opponent rating caused a particular tournament score.