Introduction

This project uses a text file of chess tournament results with 64 players and 7 rounds (tournamentinfo.txt). The file has some structure but is not a ready-to-use table. The goal is to turn it into a CSV file, which could be imported into a SQL database, with each player’s name, state, total points, pre-rating, and the average pre-rating of their opponents.

Approach

Each player takes up two lines in the file, separated by lines of dashes. The first line has the pair number, name, total points, and the 7 round results. The second line has the state and the ratings. I remove the separator lines, pair up the two lines for each player, split them on the | character, and extract each field with regular expressions. I then look up each opponent’s pre-rating by pair number to calculate the average.

Load the Data

The data is read from my GitHub repository, so the code does not depend on a local file path.

library(tidyverse)

data_url <- "https://raw.githubusercontent.com/YaldaAzamee/data607-project1/main/tournamentinfo.txt"
raw <- read_lines(data_url)

head(raw, 7)
## [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] "-----------------------------------------------------------------------------------------"

Separate the Two Lines for Each Player

The separator lines contain only dashes. After removing them, the first two lines are the column headers, which I also drop. The remaining lines alternate between each player’s first and second line.

player_lines <- raw[!str_detect(raw, "^-+$")][-(1:2)]

line1 <- player_lines[seq(1, length(player_lines), by = 2)]
line2 <- player_lines[seq(2, length(player_lines), by = 2)]

# Both should have one line per player (64)
length(line1)
## [1] 64
length(line2)
## [1] 64

Extract Player Information

I split each line on | and trim the extra spaces. Some pre-ratings are provisional (for example R: 1641P17), so I keep only the digits right after R:.

f1 <- str_split(line1, "\\|", simplify = TRUE)
f2 <- str_split(line2, "\\|", simplify = TRUE)

players <- tibble(
  pair       = as.integer(str_trim(f1[, 1])),
  name       = str_to_title(str_trim(f1[, 2])),
  state      = str_trim(f2[, 1]),
  points     = as.numeric(str_trim(f1[, 3])),
  pre_rating = as.integer(str_match(f2[, 2], "R:\\s*(\\d+)")[, 2])
)

head(players)
## # A tibble: 6 × 5
##    pair name                state points pre_rating
##   <int> <chr>               <chr>  <dbl>      <int>
## 1     1 Gary Hua            ON       6         1794
## 2     2 Dakshesh Daruri     MI       6         1553
## 3     3 Aditya Bajaj        MI       6         1384
## 4     4 Patrick H Schilling MI       5.5       1716
## 5     5 Hanshi Zuo          MI       5.5       1655
## 6     6 Hansen Song         OH       5         1686

Average Pre-Rating of Opponents

Columns 4 to 10 of the first line hold the 7 rounds, for example W 39 (a win against player 39). Only wins, losses, and draws (W, L, D) have an opponent. Byes and unplayed rounds (H, U, B, X) have no opponent, so they are not counted, and the average is divided by the number of games actually played.

rounds <- f1[, 4:10]

opponents <- apply(rounds, 1, function(r) {
  as.integer(str_match(r, "^[WLD]\\s+(\\d+)")[, 2]) |> na.omit()
})

players$avg_opp_rating <- map_dbl(opponents, function(opp) {
  round(mean(players$pre_rating[match(opp, players$pair)]))
})

To check the calculation, the first player should match the example in the instructions: Gary Hua, ON, 6.0, 1794, 1605.

players |> filter(pair == 1)
## # A tibble: 1 × 6
##    pair name     state points pre_rating avg_opp_rating
##   <int> <chr>    <chr>  <dbl>      <int>          <dbl>
## 1     1 Gary Hua ON         6       1794           1605
stopifnot(players$avg_opp_rating[players$pair == 1] == 1605)

Final Table and CSV

result <- players |>
  transmute(
    `Player Name`                 = name,
    `Player State`                = state,
    `Total Points`                = sprintf("%.1f", points),  # keeps one decimal, e.g. 6.0
    `Pre-Rating`                  = pre_rating,
    `Average Opponent Pre-Rating` = avg_opp_rating
  )

knitr::kable(result)
Player Name Player State Total Points Pre-Rating Average Opponent Pre-Rating
Gary Hua ON 6.0 1794 1605
Dakshesh Daruri MI 6.0 1553 1469
Aditya Bajaj MI 6.0 1384 1564
Patrick H Schilling MI 5.5 1716 1574
Hanshi Zuo MI 5.5 1655 1501
Hansen Song OH 5.0 1686 1519
Gary Dee Swathell MI 5.0 1649 1372
Ezekiel Houghton MI 5.0 1641 1468
Stefano Lee ON 5.0 1411 1523
Anvit Rao MI 5.0 1365 1554
Cameron William Mc Leman MI 4.5 1712 1468
Kenneth J Tack MI 4.5 1663 1506
Torrance Henry Jr MI 4.5 1666 1498
Bradley Shaw MI 4.5 1610 1515
Zachary James Houghton MI 4.5 1220 1484
Mike Nikitin MI 4.0 1604 1386
Ronald Grzegorczyk MI 4.0 1629 1499
David Sundeen MI 4.0 1600 1480
Dipankar Roy MI 4.0 1564 1426
Jason Zheng MI 4.0 1595 1411
Dinh Dang Bui ON 4.0 1563 1470
Eugene L Mcclure MI 4.0 1555 1300
Alan Bui ON 4.0 1363 1214
Michael R Aldrich MI 4.0 1229 1357
Loren Schwiebert MI 3.5 1745 1363
Max Zhu ON 3.5 1579 1507
Gaurav Gidwani MI 3.5 1552 1222
Sofia Adina Stanescu-Bellu MI 3.5 1507 1522
Chiedozie Okorie MI 3.5 1602 1314
George Avery Jones ON 3.5 1522 1144
Rishi Shetty MI 3.5 1494 1260
Joshua Philip Mathews ON 3.5 1441 1379
Jade Ge MI 3.5 1449 1277
Michael Jeffery Thomas MI 3.5 1399 1375
Joshua David Lee MI 3.5 1438 1150
Siddharth Jha MI 3.5 1355 1388
Amiyatosh Pwnanandam MI 3.5 980 1385
Brian Liu MI 3.0 1423 1539
Joel R Hendon MI 3.0 1436 1430
Forest Zhang MI 3.0 1348 1391
Kyle William Murphy MI 3.0 1403 1248
Jared Ge MI 3.0 1332 1150
Robert Glen Vasey MI 3.0 1283 1107
Justin D Schilling MI 3.0 1199 1327
Derek Yan MI 3.0 1242 1152
Jacob Alexander Lavalley MI 3.0 377 1358
Eric Wright MI 2.5 1362 1392
Daniel Khain MI 2.5 1382 1356
Michael J Martin MI 2.5 1291 1286
Shivam Jha MI 2.5 1056 1296
Tejas Ayyagari MI 2.5 1011 1356
Ethan Guo MI 2.5 935 1495
Jose C Ybarra MI 2.0 1393 1345
Larry Hodge MI 2.0 1270 1206
Alex Kong MI 2.0 1186 1406
Marisa Ricci MI 2.0 1153 1414
Michael Lu MI 2.0 1092 1363
Viraj Mohile MI 2.0 917 1391
Sean M Mc Cormick MI 2.0 853 1319
Julia Shen MI 1.5 967 1330
Jezzel Farkas ON 1.5 955 1327
Ashwin Balaji MI 1.0 1530 1186
Thomas Joseph Hosmer MI 1.0 1175 1350
Ben Li MI 1.0 1163 1263
write_csv(result, "chess_results.csv")

The file chess_results.csv is saved in the working directory. It has 64 rows (one per player) and the five required columns, and it has no missing values: 0 missing.

Conclusions

Findings

  • The text file was converted into a clean table of 64 players, and the result for the first player matches the example in the instructions.
  • Gary Hua faced the strongest opponents, with an average opponent pre-rating of 1605.
  • Players with higher pre-ratings tended to score more points (correlation = 0.61), but there were surprises. For example, Aditya Bajaj had a pre-rating of only 1384 but finished with 6.0 points, tied for the top score.

AI Use

AI-ChatGPT was used to understand the assignment and follow through steps.