library(knitr)
library(ggplot2)
library(forcats)
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

In this project, you’re given a text file with chess tournament results where the information has some structure. Your job is to create an R Markdown file that generates a .CSV file (that could for example be imported into a SQL database) with the following information for all of the players:

Load Tournament.txt from Github

https://raw.githubusercontent.com/prnakyazze94/Data_607/refs/heads/main/Class%20Tournament.txt

# Read the file
lines <- readLines("https://raw.githubusercontent.com/prnakyazze94/Data_607/refs/heads/main/Class%20Tournament.txt")
print(lines)
##   [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|" 
##  [21] "   OH | 15055204 / R: 1686   ->1687     |N:3  |W    |B    |W    |B    |B    |W    |B    |" 
##  [22] "-----------------------------------------------------------------------------------------" 
##  [23] "    7 | GARY DEE SWATHELL               |5.0  |W  57|W  46|W  13|W  11|L   1|W   9|L   2|" 
##  [24] "   MI | 11146376 / R: 1649   ->1673     |N:3  |W    |B    |W    |B    |B    |W    |W    |" 
##  [25] "-----------------------------------------------------------------------------------------" 
##  [26] "    8 | EZEKIEL HOUGHTON                |5.0  |W   3|W  32|L  14|L   9|W  47|W  28|W  19|" 
##  [27] "   MI | 15142253 / R: 1641P17->1657P24  |N:3  |B    |W    |B    |W    |B    |W    |W    |" 
##  [28] "-----------------------------------------------------------------------------------------" 
##  [29] "    9 | STEFANO LEE                     |5.0  |W  25|L  18|W  59|W   8|W  26|L   7|W  20|" 
##  [30] "   ON | 14954524 / R: 1411   ->1564     |N:2  |W    |B    |W    |B    |W    |B    |B    |" 
##  [31] "-----------------------------------------------------------------------------------------" 
##  [32] "   10 | ANVIT RAO                       |5.0  |D  16|L  19|W  55|W  31|D   6|W  25|W  18|" 
##  [33] "   MI | 14150362 / R: 1365   ->1544     |N:3  |W    |W    |B    |B    |W    |B    |W    |" 
##  [34] "-----------------------------------------------------------------------------------------" 
##  [35] "   11 | CAMERON WILLIAM MC LEMAN        |4.5  |D  38|W  56|W   6|L   7|L   3|W  34|W  26|" 
##  [36] "   MI | 12581589 / R: 1712   ->1696     |N:3  |B    |W    |B    |W    |B    |W    |B    |" 
##  [37] "-----------------------------------------------------------------------------------------" 
##  [38] "   12 | KENNETH J TACK                  |4.5  |W  42|W  33|D   5|W  38|H    |D   1|L   3|" 
##  [39] "   MI | 12681257 / R: 1663   ->1670     |N:3  |W    |B    |W    |B    |     |W    |B    |" 
##  [40] "-----------------------------------------------------------------------------------------" 
##  [41] "   13 | TORRANCE HENRY JR               |4.5  |W  36|W  27|L   7|D   5|W  33|L   3|W  32|" 
##  [42] "   MI | 15082995 / R: 1666   ->1662     |N:3  |B    |W    |B    |B    |W    |W    |B    |" 
##  [43] "-----------------------------------------------------------------------------------------" 
##  [44] "   14 | BRADLEY SHAW                    |4.5  |W  54|W  44|W   8|L   1|D  27|L   5|W  31|" 
##  [45] "   MI | 10131499 / R: 1610   ->1618     |N:3  |W    |B    |W    |W    |B    |B    |W    |" 
##  [46] "-----------------------------------------------------------------------------------------" 
##  [47] "   15 | ZACHARY JAMES HOUGHTON          |4.5  |D  19|L  16|W  30|L  22|W  54|W  33|W  38|" 
##  [48] "   MI | 15619130 / R: 1220P13->1416P20  |N:3  |B    |B    |W    |W    |B    |B    |W    |" 
##  [49] "-----------------------------------------------------------------------------------------" 
##  [50] "   16 | MIKE NIKITIN                    |4.0  |D  10|W  15|H    |W  39|L   2|W  36|U    |" 
##  [51] "   MI | 10295068 / R: 1604   ->1613     |N:3  |B    |W    |     |B    |W    |B    |     |" 
##  [52] "-----------------------------------------------------------------------------------------" 
##  [53] "   17 | RONALD GRZEGORCZYK              |4.0  |W  48|W  41|L  26|L   2|W  23|W  22|L   5|" 
##  [54] "   MI | 10297702 / R: 1629   ->1610     |N:3  |W    |B    |W    |B    |W    |B    |W    |" 
##  [55] "-----------------------------------------------------------------------------------------" 
##  [56] "   18 | DAVID SUNDEEN                   |4.0  |W  47|W   9|L   1|W  32|L  19|W  38|L  10|" 
##  [57] "   MI | 11342094 / R: 1600   ->1600     |N:3  |B    |W    |B    |W    |B    |W    |B    |" 
##  [58] "-----------------------------------------------------------------------------------------" 
##  [59] "   19 | DIPANKAR ROY                    |4.0  |D  15|W  10|W  52|D  28|W  18|L   4|L   8|" 
##  [60] "   MI | 14862333 / R: 1564   ->1570     |N:3  |W    |B    |W    |B    |W    |W    |B    |" 
##  [61] "-----------------------------------------------------------------------------------------" 
##  [62] "   20 | JASON ZHENG                     |4.0  |L  40|W  49|W  23|W  41|W  28|L   2|L   9|" 
##  [63] "   MI | 14529060 / R: 1595   ->1569     |N:4  |W    |B    |W    |B    |W    |B    |W    |" 
##  [64] "-----------------------------------------------------------------------------------------" 
##  [65] "   21 | DINH DANG BUI                   |4.0  |W  43|L   1|W  47|L   3|W  40|W  39|L   6|" 
##  [66] "   ON | 15495066 / R: 1563P22->1562     |N:3  |B    |W    |B    |W    |W    |B    |W    |" 
##  [67] "-----------------------------------------------------------------------------------------" 
##  [68] "   22 | EUGENE L MCCLURE                |4.0  |W  64|D  52|L  28|W  15|H    |L  17|W  40|" 
##  [69] "   MI | 12405534 / R: 1555   ->1529     |N:4  |W    |B    |W    |B    |     |W    |B    |" 
##  [70] "-----------------------------------------------------------------------------------------" 
##  [71] "   23 | ALAN BUI                        |4.0  |L   4|W  43|L  20|W  58|L  17|W  37|W  46|" 
##  [72] "   ON | 15030142 / R: 1363   ->1371     |     |B    |W    |B    |W    |B    |W    |B    |" 
##  [73] "-----------------------------------------------------------------------------------------" 
##  [74] "   24 | MICHAEL R ALDRICH               |4.0  |L  28|L  47|W  43|L  25|W  60|W  44|W  39|" 
##  [75] "   MI | 13469010 / R: 1229   ->1300     |N:4  |B    |W    |B    |B    |W    |W    |B    |" 
##  [76] "-----------------------------------------------------------------------------------------" 
##  [77] "   25 | LOREN SCHWIEBERT                |3.5  |L   9|W  53|L   3|W  24|D  34|L  10|W  47|" 
##  [78] "   MI | 12486656 / R: 1745   ->1681     |N:4  |B    |W    |B    |W    |B    |W    |B    |" 
##  [79] "-----------------------------------------------------------------------------------------" 
##  [80] "   26 | MAX ZHU                         |3.5  |W  49|W  40|W  17|L   4|L   9|D  32|L  11|" 
##  [81] "   ON | 15131520 / R: 1579   ->1564     |N:4  |B    |W    |B    |W    |B    |W    |W    |" 
##  [82] "-----------------------------------------------------------------------------------------" 
##  [83] "   27 | GAURAV GIDWANI                  |3.5  |W  51|L  13|W  46|W  37|D  14|L   6|U    |" 
##  [84] "   MI | 14476567 / R: 1552   ->1539     |N:4  |W    |B    |W    |B    |W    |B    |     |" 
##  [85] "-----------------------------------------------------------------------------------------" 
##  [86] "   28 | SOFIA ADINA STANESCU-BELLU      |3.5  |W  24|D   4|W  22|D  19|L  20|L   8|D  36|" 
##  [87] "   MI | 14882954 / R: 1507   ->1513     |N:3  |W    |W    |B    |W    |B    |B    |W    |" 
##  [88] "-----------------------------------------------------------------------------------------" 
##  [89] "   29 | CHIEDOZIE OKORIE                |3.5  |W  50|D   6|L  38|L  34|W  52|W  48|U    |" 
##  [90] "   MI | 15323285 / R: 1602P6 ->1508P12  |N:4  |B    |W    |B    |W    |W    |B    |     |" 
##  [91] "-----------------------------------------------------------------------------------------" 
##  [92] "   30 | GEORGE AVERY JONES              |3.5  |L  52|D  64|L  15|W  55|L  31|W  61|W  50|" 
##  [93] "   ON | 12577178 / R: 1522   ->1444     |     |W    |B    |B    |W    |W    |B    |B    |" 
##  [94] "-----------------------------------------------------------------------------------------" 
##  [95] "   31 | RISHI SHETTY                    |3.5  |L  58|D  55|W  64|L  10|W  30|W  50|L  14|" 
##  [96] "   MI | 15131618 / R: 1494   ->1444     |     |B    |W    |B    |W    |B    |W    |B    |" 
##  [97] "-----------------------------------------------------------------------------------------" 
##  [98] "   32 | JOSHUA PHILIP MATHEWS           |3.5  |W  61|L   8|W  44|L  18|W  51|D  26|L  13|" 
##  [99] "   ON | 14073750 / R: 1441   ->1433     |N:4  |W    |B    |W    |B    |W    |B    |W    |" 
## [100] "-----------------------------------------------------------------------------------------" 
## [101] "   33 | JADE GE                         |3.5  |W  60|L  12|W  50|D  36|L  13|L  15|W  51|" 
## [102] "   MI | 14691842 / R: 1449   ->1421     |     |B    |W    |B    |W    |B    |W    |B    |" 
## [103] "-----------------------------------------------------------------------------------------" 
## [104] "   34 | MICHAEL JEFFERY THOMAS          |3.5  |L   6|W  60|L  37|W  29|D  25|L  11|W  52|" 
## [105] "   MI | 15051807 / R: 1399   ->1400     |     |B    |W    |B    |B    |W    |B    |W    |" 
## [106] "-----------------------------------------------------------------------------------------" 
## [107] "   35 | JOSHUA DAVID LEE                |3.5  |L  46|L  38|W  56|L   6|W  57|D  52|W  48|" 
## [108] "   MI | 14601397 / R: 1438   ->1392     |     |W    |W    |B    |W    |B    |B    |W    |" 
## [109] "-----------------------------------------------------------------------------------------" 
## [110] "   36 | SIDDHARTH JHA                   |3.5  |L  13|W  57|W  51|D  33|H    |L  16|D  28|" 
## [111] "   MI | 14773163 / R: 1355   ->1367     |N:4  |W    |B    |W    |B    |     |W    |B    |" 
## [112] "-----------------------------------------------------------------------------------------" 
## [113] "   37 | AMIYATOSH PWNANANDAM            |3.5  |B    |L   5|W  34|L  27|H    |L  23|W  61|" 
## [114] "   MI | 15489571 / R:  980P12->1077P17  |     |     |B    |W    |W    |     |B    |W    |" 
## [115] "-----------------------------------------------------------------------------------------" 
## [116] "   38 | BRIAN LIU                       |3.0  |D  11|W  35|W  29|L  12|H    |L  18|L  15|" 
## [117] "   MI | 15108523 / R: 1423   ->1439     |N:4  |W    |B    |W    |W    |     |B    |B    |" 
## [118] "-----------------------------------------------------------------------------------------" 
## [119] "   39 | JOEL R HENDON                   |3.0  |L   1|W  54|W  40|L  16|W  44|L  21|L  24|" 
## [120] "   MI | 12923035 / R: 1436P23->1413     |N:4  |B    |W    |B    |W    |B    |W    |W    |" 
## [121] "-----------------------------------------------------------------------------------------" 
## [122] "   40 | FOREST ZHANG                    |3.0  |W  20|L  26|L  39|W  59|L  21|W  56|L  22|" 
## [123] "   MI | 14892710 / R: 1348   ->1346     |     |B    |B    |W    |W    |B    |W    |W    |" 
## [124] "-----------------------------------------------------------------------------------------" 
## [125] "   41 | KYLE WILLIAM MURPHY             |3.0  |W  59|L  17|W  58|L  20|X    |U    |U    |" 
## [126] "   MI | 15761443 / R: 1403P5 ->1341P9   |     |B    |W    |B    |W    |     |     |     |" 
## [127] "-----------------------------------------------------------------------------------------" 
## [128] "   42 | JARED GE                        |3.0  |L  12|L  50|L  57|D  60|D  61|W  64|W  56|" 
## [129] "   MI | 14462326 / R: 1332   ->1256     |     |B    |W    |B    |B    |W    |W    |B    |" 
## [130] "-----------------------------------------------------------------------------------------" 
## [131] "   43 | ROBERT GLEN VASEY               |3.0  |L  21|L  23|L  24|W  63|W  59|L  46|W  55|" 
## [132] "   MI | 14101068 / R: 1283   ->1244     |     |W    |B    |W    |W    |B    |B    |W    |" 
## [133] "-----------------------------------------------------------------------------------------" 
## [134] "   44 | JUSTIN D SCHILLING              |3.0  |B    |L  14|L  32|W  53|L  39|L  24|W  59|" 
## [135] "   MI | 15323504 / R: 1199   ->1199     |     |     |W    |B    |B    |W    |B    |W    |" 
## [136] "-----------------------------------------------------------------------------------------" 
## [137] "   45 | DEREK YAN                       |3.0  |L   5|L  51|D  60|L  56|W  63|D  55|W  58|" 
## [138] "   MI | 15372807 / R: 1242   ->1191     |     |W    |B    |W    |B    |W    |B    |W    |" 
## [139] "-----------------------------------------------------------------------------------------" 
## [140] "   46 | JACOB ALEXANDER LAVALLEY        |3.0  |W  35|L   7|L  27|L  50|W  64|W  43|L  23|" 
## [141] "   MI | 15490981 / R:  377P3 ->1076P10  |     |B    |W    |B    |W    |B    |W    |W    |" 
## [142] "-----------------------------------------------------------------------------------------" 
## [143] "   47 | ERIC WRIGHT                     |2.5  |L  18|W  24|L  21|W  61|L   8|D  51|L  25|" 
## [144] "   MI | 12533115 / R: 1362   ->1341     |     |W    |B    |W    |B    |W    |B    |W    |" 
## [145] "-----------------------------------------------------------------------------------------" 
## [146] "   48 | DANIEL KHAIN                    |2.5  |L  17|W  63|H    |D  52|H    |L  29|L  35|" 
## [147] "   MI | 14369165 / R: 1382   ->1335     |     |B    |W    |     |B    |     |W    |B    |" 
## [148] "-----------------------------------------------------------------------------------------" 
## [149] "   49 | MICHAEL J MARTIN                |2.5  |L  26|L  20|D  63|D  64|W  58|H    |U    |" 
## [150] "   MI | 12531685 / R: 1291P12->1259P17  |     |W    |W    |B    |W    |B    |     |     |" 
## [151] "-----------------------------------------------------------------------------------------" 
## [152] "   50 | SHIVAM JHA                      |2.5  |L  29|W  42|L  33|W  46|H    |L  31|L  30|" 
## [153] "   MI | 14773178 / R: 1056   ->1111     |     |W    |B    |W    |B    |     |B    |W    |" 
## [154] "-----------------------------------------------------------------------------------------" 
## [155] "   51 | TEJAS AYYAGARI                  |2.5  |L  27|W  45|L  36|W  57|L  32|D  47|L  33|" 
## [156] "   MI | 15205474 / R: 1011   ->1097     |     |B    |W    |B    |W    |B    |W    |W    |" 
## [157] "-----------------------------------------------------------------------------------------" 
## [158] "   52 | ETHAN GUO                       |2.5  |W  30|D  22|L  19|D  48|L  29|D  35|L  34|" 
## [159] "   MI | 14918803 / R:  935   ->1092     |N:4  |B    |W    |B    |W    |B    |W    |B    |" 
## [160] "-----------------------------------------------------------------------------------------" 
## [161] "   53 | JOSE C YBARRA                   |2.0  |H    |L  25|H    |L  44|U    |W  57|U    |" 
## [162] "   MI | 12578849 / R: 1393   ->1359     |     |     |B    |     |W    |     |W    |     |" 
## [163] "-----------------------------------------------------------------------------------------" 
## [164] "   54 | LARRY HODGE                     |2.0  |L  14|L  39|L  61|B    |L  15|L  59|W  64|" 
## [165] "   MI | 12836773 / R: 1270   ->1200     |     |B    |B    |W    |     |W    |B    |W    |" 
## [166] "-----------------------------------------------------------------------------------------" 
## [167] "   55 | ALEX KONG                       |2.0  |L  62|D  31|L  10|L  30|B    |D  45|L  43|" 
## [168] "   MI | 15412571 / R: 1186   ->1163     |     |W    |B    |W    |B    |     |W    |B    |" 
## [169] "-----------------------------------------------------------------------------------------" 
## [170] "   56 | MARISA RICCI                    |2.0  |H    |L  11|L  35|W  45|H    |L  40|L  42|" 
## [171] "   MI | 14679887 / R: 1153   ->1140     |     |     |B    |W    |W    |     |B    |W    |" 
## [172] "-----------------------------------------------------------------------------------------" 
## [173] "   57 | MICHAEL LU                      |2.0  |L   7|L  36|W  42|L  51|L  35|L  53|B    |" 
## [174] "   MI | 15113330 / R: 1092   ->1079     |     |B    |W    |W    |B    |W    |B    |     |" 
## [175] "-----------------------------------------------------------------------------------------" 
## [176] "   58 | VIRAJ MOHILE                    |2.0  |W  31|L   2|L  41|L  23|L  49|B    |L  45|" 
## [177] "   MI | 14700365 / R:  917   -> 941     |     |W    |B    |W    |B    |W    |     |B    |" 
## [178] "-----------------------------------------------------------------------------------------" 
## [179] "   59 | SEAN M MC CORMICK               |2.0  |L  41|B    |L   9|L  40|L  43|W  54|L  44|" 
## [180] "   MI | 12841036 / R:  853   -> 878     |     |W    |     |B    |B    |W    |W    |B    |" 
## [181] "-----------------------------------------------------------------------------------------" 
## [182] "   60 | JULIA SHEN                      |1.5  |L  33|L  34|D  45|D  42|L  24|H    |U    |" 
## [183] "   MI | 14579262 / R:  967   -> 984     |     |W    |B    |B    |W    |B    |     |     |" 
## [184] "-----------------------------------------------------------------------------------------" 
## [185] "   61 | JEZZEL FARKAS                   |1.5  |L  32|L   3|W  54|L  47|D  42|L  30|L  37|" 
## [186] "   ON | 15771592 / R:  955P11-> 979P18  |     |B    |W    |B    |W    |B    |W    |B    |" 
## [187] "-----------------------------------------------------------------------------------------" 
## [188] "   62 | ASHWIN BALAJI                   |1.0  |W  55|U    |U    |U    |U    |U    |U    |" 
## [189] "   MI | 15219542 / R: 1530   ->1535     |     |B    |     |     |     |     |     |     |" 
## [190] "-----------------------------------------------------------------------------------------" 
## [191] "   63 | THOMAS JOSEPH HOSMER            |1.0  |L   2|L  48|D  49|L  43|L  45|H    |U    |" 
## [192] "   MI | 15057092 / R: 1175   ->1125     |     |W    |B    |W    |B    |B    |     |     |" 
## [193] "-----------------------------------------------------------------------------------------" 
## [194] "   64 | BEN LI                          |1.0  |L  22|D  30|L  31|D  49|L  46|L  42|L  54|" 
## [195] "   MI | 15006561 / R: 1163   ->1112     |     |B    |W    |W    |B    |W    |B    |B    |" 
## [196] "-----------------------------------------------------------------------------------------"

Filter only player lines information.

# Remove separator lines and blank lines
lines <- lines[!grepl("^-+|^\\s*$", lines)]

# Remove the two header lines
lines <- lines[-c(1, 2)]

# Separate into player + info lines
player_lines <- lines[seq(1, length(lines), by = 2)]
info_lines <- lines[seq(2, length(lines), by = 2)]

create df

results <- data.frame(
  PairNum = integer(),
  Name = character(),
  State = character(),
  USCF_ID = character(),
  PreRating = numeric(),
  PostRating = numeric(),
  TotalScore = numeric(),
  stringsAsFactors = FALSE
)

Player’s Name, Player’s State, Total Number of Points, Player’s Pre-Rating, and Average Pre Chess Rating of Opponents For the first player, the information would be: Gary Hua, ON, 6.0, 1794, 1605

Extract all Player information, calculate Average(mean) and Prepare output data frame

# Extract all pre-ratings once (used for AvgOpponentRating)
pre_ratings <- sapply(info_lines, function(info) {
  rating_match <- regmatches(info, regexpr("R:\\s*\\d+", info))
  as.integer(gsub("R:\\s*", "", rating_match))
})

# Prepare output data frame
output <- data.frame(
  Player = character(),
  State = character(),
  Points = numeric(),
  PreRating = integer(),
  AvgOpponentRating = numeric(),
  PlayerNum = integer(),
  OpponentNums = character(),
  OpponentPreRatings = character(),
  stringsAsFactors = FALSE
)

# Loop through players
for (i in seq_along(player_lines)) {
  pl <- player_lines[i]
  info <- info_lines[i]

  # Extract clean player name (remove leading number and pipe)
  name <- trimws(gsub("^\\d+\\s*\\|\\s*", "", substr(pl, 5, 36)))

  # Extract points
  points_match <- regmatches(pl, regexpr("\\|\\s*[0-9]+\\.?[0-9]*\\s*\\|", pl))
  points <- as.numeric(gsub("[^0-9.]", "", points_match))

  # Extract state (e.g., ON, MI)
  state_match <- regmatches(info, regexpr("\\b[A-Z]{2}\\b", info))
  state <- if (length(state_match) > 0) state_match else NA

  # Extract pre-rating
  rating_match <- regmatches(info, regexpr("R:\\s*\\d+", info))
  pre_rating <- as.integer(gsub("R:\\s*", "", rating_match))

  # Extract opponent indices from line 1 (rounds)
  rounds <- unlist(strsplit(pl, "\\|"))
  rounds <- rounds[4:length(rounds)]  # skip first 3 parts
  opp_indices <- as.integer(gsub("[^0-9]", "", rounds))
  opp_indices <- opp_indices[!is.na(opp_indices)]

  # Calculate average opponent rating
  if (length(opp_indices) > 0) {
    opp_ratings <- pre_ratings[opp_indices]
    avg_opp_rating <- round(mean(opp_ratings, na.rm = TRUE), 0)
  } else {
    avg_opp_rating <- NA
  }

  # Add row to output
  output <- rbind(output, data.frame(
    Player = name,
    State = state,
    Points = points,
    PreRating = pre_rating,
    AvgOpponentRating = avg_opp_rating,
    stringsAsFactors = FALSE
  ))
}

# View output
print(output)
##                        Player State Points PreRating AvgOpponentRating
## 1                    GARY HUA    ON    6.0      1794              1605
## 2             DAKSHESH DARURI    MI    6.0      1553              1469
## 3                ADITYA BAJAJ    MI    6.0      1384              1564
## 4         PATRICK H SCHILLING    MI    5.5      1716              1574
## 5                  HANSHI ZUO    MI    5.5      1655              1501
## 6                 HANSEN SONG    OH    5.0      1686              1519
## 7           GARY DEE SWATHELL    MI    5.0      1649              1372
## 8            EZEKIEL HOUGHTON    MI    5.0      1641              1468
## 9                 STEFANO LEE    ON    5.0      1411              1523
## 10                  ANVIT RAO    MI    5.0      1365              1554
## 11   CAMERON WILLIAM MC LEMAN    MI    4.5      1712              1468
## 12             KENNETH J TACK    MI    4.5      1663              1506
## 13          TORRANCE HENRY JR    MI    4.5      1666              1498
## 14               BRADLEY SHAW    MI    4.5      1610              1515
## 15     ZACHARY JAMES HOUGHTON    MI    4.5      1220              1484
## 16               MIKE NIKITIN    MI    4.0      1604              1386
## 17         RONALD GRZEGORCZYK    MI    4.0      1629              1499
## 18              DAVID SUNDEEN    MI    4.0      1600              1480
## 19               DIPANKAR ROY    MI    4.0      1564              1426
## 20                JASON ZHENG    MI    4.0      1595              1411
## 21              DINH DANG BUI    ON    4.0      1563              1470
## 22           EUGENE L MCCLURE    MI    4.0      1555              1300
## 23                   ALAN BUI    ON    4.0      1363              1214
## 24          MICHAEL R ALDRICH    MI    4.0      1229              1357
## 25           LOREN SCHWIEBERT    MI    3.5      1745              1363
## 26                    MAX ZHU    ON    3.5      1579              1507
## 27             GAURAV GIDWANI    MI    3.5      1552              1222
## 28 SOFIA ADINA STANESCU-BELLU    MI    3.5      1507              1522
## 29           CHIEDOZIE OKORIE    MI    3.5      1602              1314
## 30         GEORGE AVERY JONES    ON    3.5      1522              1144
## 31               RISHI SHETTY    MI    3.5      1494              1260
## 32      JOSHUA PHILIP MATHEWS    ON    3.5      1441              1379
## 33                    JADE GE    MI    3.5      1449              1277
## 34     MICHAEL JEFFERY THOMAS    MI    3.5      1399              1375
## 35           JOSHUA DAVID LEE    MI    3.5      1438              1150
## 36              SIDDHARTH JHA    MI    3.5      1355              1388
## 37       AMIYATOSH PWNANANDAM    MI    3.5       980              1385
## 38                  BRIAN LIU    MI    3.0      1423              1539
## 39              JOEL R HENDON    MI    3.0      1436              1430
## 40               FOREST ZHANG    MI    3.0      1348              1391
## 41        KYLE WILLIAM MURPHY    MI    3.0      1403              1248
## 42                   JARED GE    MI    3.0      1332              1150
## 43          ROBERT GLEN VASEY    MI    3.0      1283              1107
## 44         JUSTIN D SCHILLING    MI    3.0      1199              1327
## 45                  DEREK YAN    MI    3.0      1242              1152
## 46   JACOB ALEXANDER LAVALLEY    MI    3.0       377              1358
## 47                ERIC WRIGHT    MI    2.5      1362              1392
## 48               DANIEL KHAIN    MI    2.5      1382              1356
## 49           MICHAEL J MARTIN    MI    2.5      1291              1286
## 50                 SHIVAM JHA    MI    2.5      1056              1296
## 51             TEJAS AYYAGARI    MI    2.5      1011              1356
## 52                  ETHAN GUO    MI    2.5       935              1495
## 53              JOSE C YBARRA    MI    2.0      1393              1345
## 54                LARRY HODGE    MI    2.0      1270              1206
## 55                  ALEX KONG    MI    2.0      1186              1406
## 56               MARISA RICCI    MI    2.0      1153              1414
## 57                 MICHAEL LU    MI    2.0      1092              1363
## 58               VIRAJ MOHILE    MI    2.0       917              1391
## 59          SEAN M MC CORMICK    MI    2.0       853              1319
## 60                 JULIA SHEN    MI    1.5       967              1330
## 61              JEZZEL FARKAS    ON    1.5       955              1327
## 62              ASHWIN BALAJI    MI    1.0      1530              1186
## 63       THOMAS JOSEPH HOSMER    MI    1.0      1175              1350
## 64                     BEN LI    MI    1.0      1163              1263
# save to CSV
write.csv(output, "tournament_results.csv", row.names = FALSE)

Create an output2 DF with the player Num, player’s PreRTG, Opponent Nums, Opponent’s PreRTG and Average Opponent Rating.

# Create output data frame with player num and Opponent's rating and Opponents Num and Avg colm
output2 <- data.frame(
  PlayerNum = integer(),
  Player = character(),
  PreRating = integer(),
  OpponentNums = character(),
  OpponentPreRatings = character(),
  AvgOpponentRating = numeric(),
  stringsAsFactors = FALSE
)

# Extract all pre-ratings once
pre_ratings <- sapply(info_lines, function(info) {
  rating_match <- regmatches(info, regexpr("R:\\s*\\d+", info))
  as.integer(gsub("R:\\s*", "", rating_match))
})

# Loop through each player
for (i in seq_along(player_lines)) {
  pl <- player_lines[i]
  info <- info_lines[i]

  # Extract player number
  player_num <- as.integer(substr(pl, 1, 4))

  # Extract player name
  name <- trimws(gsub("^\\d+\\s*\\|\\s*", "", substr(pl, 5, 36)))

  # Extract pre-rating
  rating_match <- regmatches(info, regexpr("R:\\s*\\d+", info))
  pre_rating <- as.integer(gsub("R:\\s*", "", rating_match))

  # Extract opponent indices (from rounds)
  rounds <- unlist(strsplit(pl, "\\|"))
  rounds <- rounds[4:length(rounds)]  # Skip name, points, etc.
  opp_indices <- as.integer(gsub("[^0-9]", "", rounds))
  opp_indices <- opp_indices[!is.na(opp_indices)]

  # Get opponent pre-ratings
  if (length(opp_indices) > 0) {
    opp_ratings <- pre_ratings[opp_indices]
    avg_opp_rating <- round(mean(opp_ratings, na.rm = TRUE), 0)
  } else {
    opp_ratings <- NA
    avg_opp_rating <- NA
  }

  # Add row to output
  output2 <- rbind(output2, data.frame(
    PlayerNum = player_num,
    Player = name,
    PreRating = pre_rating,
    OpponentNums = paste(opp_indices, collapse = ", "),
    OpponentPreRatings = paste(opp_ratings, collapse = ", "),
    AvgOpponentRating = avg_opp_rating,
    stringsAsFactors = FALSE
  ))
}

# View the output
kable(output2)
PlayerNum Player PreRating OpponentNums OpponentPreRatings AvgOpponentRating
NA GARY HUA 1794 39, 21, 18, 14, 7, 12, 4 1436, 1563, 1600, 1610, 1649, 1663, 1716 1605
NA DAKSHESH DARURI 1553 63, 58, 4, 17, 16, 20, 7 1175, 917, 1716, 1629, 1604, 1595, 1649 1469
NA ADITYA BAJAJ 1384 8, 61, 25, 21, 11, 13, 12 1641, 955, 1745, 1563, 1712, 1666, 1663 1564
NA PATRICK H SCHILLING 1716 23, 28, 2, 26, 5, 19, 1 1363, 1507, 1553, 1579, 1655, 1564, 1794 1574
NA HANSHI ZUO 1655 45, 37, 12, 13, 4, 14, 17 1242, 980, 1663, 1666, 1716, 1610, 1629 1501
NA HANSEN SONG 1686 34, 29, 11, 35, 10, 27, 21 1399, 1602, 1712, 1438, 1365, 1552, 1563 1519
NA GARY DEE SWATHELL 1649 57, 46, 13, 11, 1, 9, 2 1092, 377, 1666, 1712, 1794, 1411, 1553 1372
NA EZEKIEL HOUGHTON 1641 3, 32, 14, 9, 47, 28, 19 1384, 1441, 1610, 1411, 1362, 1507, 1564 1468
NA STEFANO LEE 1411 25, 18, 59, 8, 26, 7, 20 1745, 1600, 853, 1641, 1579, 1649, 1595 1523
1 ANVIT RAO 1365 16, 19, 55, 31, 6, 25, 18 1604, 1564, 1186, 1494, 1686, 1745, 1600 1554
1 CAMERON WILLIAM MC LEMAN 1712 38, 56, 6, 7, 3, 34, 26 1423, 1153, 1686, 1649, 1384, 1399, 1579 1468
1 KENNETH J TACK 1663 42, 33, 5, 38, 1, 3 1332, 1449, 1655, 1423, 1794, 1384 1506
1 TORRANCE HENRY JR 1666 36, 27, 7, 5, 33, 3, 32 1355, 1552, 1649, 1655, 1449, 1384, 1441 1498
1 BRADLEY SHAW 1610 54, 44, 8, 1, 27, 5, 31 1270, 1199, 1641, 1794, 1552, 1655, 1494 1515
1 ZACHARY JAMES HOUGHTON 1220 19, 16, 30, 22, 54, 33, 38 1564, 1604, 1522, 1555, 1270, 1449, 1423 1484
1 MIKE NIKITIN 1604 10, 15, 39, 2, 36 1365, 1220, 1436, 1553, 1355 1386
1 RONALD GRZEGORCZYK 1629 48, 41, 26, 2, 23, 22, 5 1382, 1403, 1579, 1553, 1363, 1555, 1655 1499
1 DAVID SUNDEEN 1600 47, 9, 1, 32, 19, 38, 10 1362, 1411, 1794, 1441, 1564, 1423, 1365 1480
1 DIPANKAR ROY 1564 15, 10, 52, 28, 18, 4, 8 1220, 1365, 935, 1507, 1600, 1716, 1641 1426
2 JASON ZHENG 1595 40, 49, 23, 41, 28, 2, 9 1348, 1291, 1363, 1403, 1507, 1553, 1411 1411
2 DINH DANG BUI 1563 43, 1, 47, 3, 40, 39, 6 1283, 1794, 1362, 1384, 1348, 1436, 1686 1470
2 EUGENE L MCCLURE 1555 64, 52, 28, 15, 17, 40 1163, 935, 1507, 1220, 1629, 1348 1300
2 ALAN BUI 1363 4, 43, 20, 58, 17, 37, 46 1716, 1283, 1595, 917, 1629, 980, 377 1214
2 MICHAEL R ALDRICH 1229 28, 47, 43, 25, 60, 44, 39 1507, 1362, 1283, 1745, 967, 1199, 1436 1357
2 LOREN SCHWIEBERT 1745 9, 53, 3, 24, 34, 10, 47 1411, 1393, 1384, 1229, 1399, 1365, 1362 1363
2 MAX ZHU 1579 49, 40, 17, 4, 9, 32, 11 1291, 1348, 1629, 1716, 1411, 1441, 1712 1507
2 GAURAV GIDWANI 1552 51, 13, 46, 37, 14, 6 1011, 1666, 377, 980, 1610, 1686 1222
2 SOFIA ADINA STANESCU-BELLU 1507 24, 4, 22, 19, 20, 8, 36 1229, 1716, 1555, 1564, 1595, 1641, 1355 1522
2 CHIEDOZIE OKORIE 1602 50, 6, 38, 34, 52, 48 1056, 1686, 1423, 1399, 935, 1382 1314
3 GEORGE AVERY JONES 1522 52, 64, 15, 55, 31, 61, 50 935, 1163, 1220, 1186, 1494, 955, 1056 1144
3 RISHI SHETTY 1494 58, 55, 64, 10, 30, 50, 14 917, 1186, 1163, 1365, 1522, 1056, 1610 1260
3 JOSHUA PHILIP MATHEWS 1441 61, 8, 44, 18, 51, 26, 13 955, 1641, 1199, 1600, 1011, 1579, 1666 1379
3 JADE GE 1449 60, 12, 50, 36, 13, 15, 51 967, 1663, 1056, 1355, 1666, 1220, 1011 1277
3 MICHAEL JEFFERY THOMAS 1399 6, 60, 37, 29, 25, 11, 52 1686, 967, 980, 1602, 1745, 1712, 935 1375
3 JOSHUA DAVID LEE 1438 46, 38, 56, 6, 57, 52, 48 377, 1423, 1153, 1686, 1092, 935, 1382 1150
3 SIDDHARTH JHA 1355 13, 57, 51, 33, 16, 28 1666, 1092, 1011, 1449, 1604, 1507 1388
3 AMIYATOSH PWNANANDAM 980 5, 34, 27, 23, 61 1655, 1399, 1552, 1363, 955 1385
3 BRIAN LIU 1423 11, 35, 29, 12, 18, 15 1712, 1438, 1602, 1663, 1600, 1220 1539
3 JOEL R HENDON 1436 1, 54, 40, 16, 44, 21, 24 1794, 1270, 1348, 1604, 1199, 1563, 1229 1430
4 FOREST ZHANG 1348 20, 26, 39, 59, 21, 56, 22 1595, 1579, 1436, 853, 1563, 1153, 1555 1391
4 KYLE WILLIAM MURPHY 1403 59, 17, 58, 20 853, 1629, 917, 1595 1248
4 JARED GE 1332 12, 50, 57, 60, 61, 64, 56 1663, 1056, 1092, 967, 955, 1163, 1153 1150
4 ROBERT GLEN VASEY 1283 21, 23, 24, 63, 59, 46, 55 1563, 1363, 1229, 1175, 853, 377, 1186 1107
4 JUSTIN D SCHILLING 1199 14, 32, 53, 39, 24, 59 1610, 1441, 1393, 1436, 1229, 853 1327
4 DEREK YAN 1242 5, 51, 60, 56, 63, 55, 58 1655, 1011, 967, 1153, 1175, 1186, 917 1152
4 JACOB ALEXANDER LAVALLEY 377 35, 7, 27, 50, 64, 43, 23 1438, 1649, 1552, 1056, 1163, 1283, 1363 1358
4 ERIC WRIGHT 1362 18, 24, 21, 61, 8, 51, 25 1600, 1229, 1563, 955, 1641, 1011, 1745 1392
4 DANIEL KHAIN 1382 17, 63, 52, 29, 35 1629, 1175, 935, 1602, 1438 1356
4 MICHAEL J MARTIN 1291 26, 20, 63, 64, 58 1579, 1595, 1175, 1163, 917 1286
5 SHIVAM JHA 1056 29, 42, 33, 46, 31, 30 1602, 1332, 1449, 377, 1494, 1522 1296
5 TEJAS AYYAGARI 1011 27, 45, 36, 57, 32, 47, 33 1552, 1242, 1355, 1092, 1441, 1362, 1449 1356
5 ETHAN GUO 935 30, 22, 19, 48, 29, 35, 34 1522, 1555, 1564, 1382, 1602, 1438, 1399 1495
5 JOSE C YBARRA 1393 25, 44, 57 1745, 1199, 1092 1345
5 LARRY HODGE 1270 14, 39, 61, 15, 59, 64 1610, 1436, 955, 1220, 853, 1163 1206
5 ALEX KONG 1186 62, 31, 10, 30, 45, 43 1530, 1494, 1365, 1522, 1242, 1283 1406
5 MARISA RICCI 1153 11, 35, 45, 40, 42 1712, 1438, 1242, 1348, 1332 1414
5 MICHAEL LU 1092 7, 36, 42, 51, 35, 53 1649, 1355, 1332, 1011, 1438, 1393 1363
5 VIRAJ MOHILE 917 31, 2, 41, 23, 49, 45 1494, 1553, 1403, 1363, 1291, 1242 1391
5 SEAN M MC CORMICK 853 41, 9, 40, 43, 54, 44 1403, 1411, 1348, 1283, 1270, 1199 1319
6 JULIA SHEN 967 33, 34, 45, 42, 24 1449, 1399, 1242, 1332, 1229 1330
6 JEZZEL FARKAS 955 32, 3, 54, 47, 42, 30, 37 1441, 1384, 1270, 1362, 1332, 1522, 980 1327
6 ASHWIN BALAJI 1530 55 1186 1186
6 THOMAS JOSEPH HOSMER 1175 2, 48, 49, 43, 45 1553, 1382, 1291, 1283, 1242 1350
6 BEN LI 1163 22, 30, 31, 49, 46, 42, 54 1555, 1522, 1494, 1291, 377, 1332, 1270 1263
kable(output2[, c("Player", "OpponentNums", "AvgOpponentRating")])
Player OpponentNums AvgOpponentRating
GARY HUA 39, 21, 18, 14, 7, 12, 4 1605
DAKSHESH DARURI 63, 58, 4, 17, 16, 20, 7 1469
ADITYA BAJAJ 8, 61, 25, 21, 11, 13, 12 1564
PATRICK H SCHILLING 23, 28, 2, 26, 5, 19, 1 1574
HANSHI ZUO 45, 37, 12, 13, 4, 14, 17 1501
HANSEN SONG 34, 29, 11, 35, 10, 27, 21 1519
GARY DEE SWATHELL 57, 46, 13, 11, 1, 9, 2 1372
EZEKIEL HOUGHTON 3, 32, 14, 9, 47, 28, 19 1468
STEFANO LEE 25, 18, 59, 8, 26, 7, 20 1523
ANVIT RAO 16, 19, 55, 31, 6, 25, 18 1554
CAMERON WILLIAM MC LEMAN 38, 56, 6, 7, 3, 34, 26 1468
KENNETH J TACK 42, 33, 5, 38, 1, 3 1506
TORRANCE HENRY JR 36, 27, 7, 5, 33, 3, 32 1498
BRADLEY SHAW 54, 44, 8, 1, 27, 5, 31 1515
ZACHARY JAMES HOUGHTON 19, 16, 30, 22, 54, 33, 38 1484
MIKE NIKITIN 10, 15, 39, 2, 36 1386
RONALD GRZEGORCZYK 48, 41, 26, 2, 23, 22, 5 1499
DAVID SUNDEEN 47, 9, 1, 32, 19, 38, 10 1480
DIPANKAR ROY 15, 10, 52, 28, 18, 4, 8 1426
JASON ZHENG 40, 49, 23, 41, 28, 2, 9 1411
DINH DANG BUI 43, 1, 47, 3, 40, 39, 6 1470
EUGENE L MCCLURE 64, 52, 28, 15, 17, 40 1300
ALAN BUI 4, 43, 20, 58, 17, 37, 46 1214
MICHAEL R ALDRICH 28, 47, 43, 25, 60, 44, 39 1357
LOREN SCHWIEBERT 9, 53, 3, 24, 34, 10, 47 1363
MAX ZHU 49, 40, 17, 4, 9, 32, 11 1507
GAURAV GIDWANI 51, 13, 46, 37, 14, 6 1222
SOFIA ADINA STANESCU-BELLU 24, 4, 22, 19, 20, 8, 36 1522
CHIEDOZIE OKORIE 50, 6, 38, 34, 52, 48 1314
GEORGE AVERY JONES 52, 64, 15, 55, 31, 61, 50 1144
RISHI SHETTY 58, 55, 64, 10, 30, 50, 14 1260
JOSHUA PHILIP MATHEWS 61, 8, 44, 18, 51, 26, 13 1379
JADE GE 60, 12, 50, 36, 13, 15, 51 1277
MICHAEL JEFFERY THOMAS 6, 60, 37, 29, 25, 11, 52 1375
JOSHUA DAVID LEE 46, 38, 56, 6, 57, 52, 48 1150
SIDDHARTH JHA 13, 57, 51, 33, 16, 28 1388
AMIYATOSH PWNANANDAM 5, 34, 27, 23, 61 1385
BRIAN LIU 11, 35, 29, 12, 18, 15 1539
JOEL R HENDON 1, 54, 40, 16, 44, 21, 24 1430
FOREST ZHANG 20, 26, 39, 59, 21, 56, 22 1391
KYLE WILLIAM MURPHY 59, 17, 58, 20 1248
JARED GE 12, 50, 57, 60, 61, 64, 56 1150
ROBERT GLEN VASEY 21, 23, 24, 63, 59, 46, 55 1107
JUSTIN D SCHILLING 14, 32, 53, 39, 24, 59 1327
DEREK YAN 5, 51, 60, 56, 63, 55, 58 1152
JACOB ALEXANDER LAVALLEY 35, 7, 27, 50, 64, 43, 23 1358
ERIC WRIGHT 18, 24, 21, 61, 8, 51, 25 1392
DANIEL KHAIN 17, 63, 52, 29, 35 1356
MICHAEL J MARTIN 26, 20, 63, 64, 58 1286
SHIVAM JHA 29, 42, 33, 46, 31, 30 1296
TEJAS AYYAGARI 27, 45, 36, 57, 32, 47, 33 1356
ETHAN GUO 30, 22, 19, 48, 29, 35, 34 1495
JOSE C YBARRA 25, 44, 57 1345
LARRY HODGE 14, 39, 61, 15, 59, 64 1206
ALEX KONG 62, 31, 10, 30, 45, 43 1406
MARISA RICCI 11, 35, 45, 40, 42 1414
MICHAEL LU 7, 36, 42, 51, 35, 53 1363
VIRAJ MOHILE 31, 2, 41, 23, 49, 45 1391
SEAN M MC CORMICK 41, 9, 40, 43, 54, 44 1319
JULIA SHEN 33, 34, 45, 42, 24 1330
JEZZEL FARKAS 32, 3, 54, 47, 42, 30, 37 1327
ASHWIN BALAJI 55 1186
THOMAS JOSEPH HOSMER 2, 48, 49, 43, 45 1350
BEN LI 22, 30, 31, 49, 46, 42, 54 1263
kable(output2[, c("Player", "OpponentPreRatings", "AvgOpponentRating")])
Player OpponentPreRatings AvgOpponentRating
GARY HUA 1436, 1563, 1600, 1610, 1649, 1663, 1716 1605
DAKSHESH DARURI 1175, 917, 1716, 1629, 1604, 1595, 1649 1469
ADITYA BAJAJ 1641, 955, 1745, 1563, 1712, 1666, 1663 1564
PATRICK H SCHILLING 1363, 1507, 1553, 1579, 1655, 1564, 1794 1574
HANSHI ZUO 1242, 980, 1663, 1666, 1716, 1610, 1629 1501
HANSEN SONG 1399, 1602, 1712, 1438, 1365, 1552, 1563 1519
GARY DEE SWATHELL 1092, 377, 1666, 1712, 1794, 1411, 1553 1372
EZEKIEL HOUGHTON 1384, 1441, 1610, 1411, 1362, 1507, 1564 1468
STEFANO LEE 1745, 1600, 853, 1641, 1579, 1649, 1595 1523
ANVIT RAO 1604, 1564, 1186, 1494, 1686, 1745, 1600 1554
CAMERON WILLIAM MC LEMAN 1423, 1153, 1686, 1649, 1384, 1399, 1579 1468
KENNETH J TACK 1332, 1449, 1655, 1423, 1794, 1384 1506
TORRANCE HENRY JR 1355, 1552, 1649, 1655, 1449, 1384, 1441 1498
BRADLEY SHAW 1270, 1199, 1641, 1794, 1552, 1655, 1494 1515
ZACHARY JAMES HOUGHTON 1564, 1604, 1522, 1555, 1270, 1449, 1423 1484
MIKE NIKITIN 1365, 1220, 1436, 1553, 1355 1386
RONALD GRZEGORCZYK 1382, 1403, 1579, 1553, 1363, 1555, 1655 1499
DAVID SUNDEEN 1362, 1411, 1794, 1441, 1564, 1423, 1365 1480
DIPANKAR ROY 1220, 1365, 935, 1507, 1600, 1716, 1641 1426
JASON ZHENG 1348, 1291, 1363, 1403, 1507, 1553, 1411 1411
DINH DANG BUI 1283, 1794, 1362, 1384, 1348, 1436, 1686 1470
EUGENE L MCCLURE 1163, 935, 1507, 1220, 1629, 1348 1300
ALAN BUI 1716, 1283, 1595, 917, 1629, 980, 377 1214
MICHAEL R ALDRICH 1507, 1362, 1283, 1745, 967, 1199, 1436 1357
LOREN SCHWIEBERT 1411, 1393, 1384, 1229, 1399, 1365, 1362 1363
MAX ZHU 1291, 1348, 1629, 1716, 1411, 1441, 1712 1507
GAURAV GIDWANI 1011, 1666, 377, 980, 1610, 1686 1222
SOFIA ADINA STANESCU-BELLU 1229, 1716, 1555, 1564, 1595, 1641, 1355 1522
CHIEDOZIE OKORIE 1056, 1686, 1423, 1399, 935, 1382 1314
GEORGE AVERY JONES 935, 1163, 1220, 1186, 1494, 955, 1056 1144
RISHI SHETTY 917, 1186, 1163, 1365, 1522, 1056, 1610 1260
JOSHUA PHILIP MATHEWS 955, 1641, 1199, 1600, 1011, 1579, 1666 1379
JADE GE 967, 1663, 1056, 1355, 1666, 1220, 1011 1277
MICHAEL JEFFERY THOMAS 1686, 967, 980, 1602, 1745, 1712, 935 1375
JOSHUA DAVID LEE 377, 1423, 1153, 1686, 1092, 935, 1382 1150
SIDDHARTH JHA 1666, 1092, 1011, 1449, 1604, 1507 1388
AMIYATOSH PWNANANDAM 1655, 1399, 1552, 1363, 955 1385
BRIAN LIU 1712, 1438, 1602, 1663, 1600, 1220 1539
JOEL R HENDON 1794, 1270, 1348, 1604, 1199, 1563, 1229 1430
FOREST ZHANG 1595, 1579, 1436, 853, 1563, 1153, 1555 1391
KYLE WILLIAM MURPHY 853, 1629, 917, 1595 1248
JARED GE 1663, 1056, 1092, 967, 955, 1163, 1153 1150
ROBERT GLEN VASEY 1563, 1363, 1229, 1175, 853, 377, 1186 1107
JUSTIN D SCHILLING 1610, 1441, 1393, 1436, 1229, 853 1327
DEREK YAN 1655, 1011, 967, 1153, 1175, 1186, 917 1152
JACOB ALEXANDER LAVALLEY 1438, 1649, 1552, 1056, 1163, 1283, 1363 1358
ERIC WRIGHT 1600, 1229, 1563, 955, 1641, 1011, 1745 1392
DANIEL KHAIN 1629, 1175, 935, 1602, 1438 1356
MICHAEL J MARTIN 1579, 1595, 1175, 1163, 917 1286
SHIVAM JHA 1602, 1332, 1449, 377, 1494, 1522 1296
TEJAS AYYAGARI 1552, 1242, 1355, 1092, 1441, 1362, 1449 1356
ETHAN GUO 1522, 1555, 1564, 1382, 1602, 1438, 1399 1495
JOSE C YBARRA 1745, 1199, 1092 1345
LARRY HODGE 1610, 1436, 955, 1220, 853, 1163 1206
ALEX KONG 1530, 1494, 1365, 1522, 1242, 1283 1406
MARISA RICCI 1712, 1438, 1242, 1348, 1332 1414
MICHAEL LU 1649, 1355, 1332, 1011, 1438, 1393 1363
VIRAJ MOHILE 1494, 1553, 1403, 1363, 1291, 1242 1391
SEAN M MC CORMICK 1403, 1411, 1348, 1283, 1270, 1199 1319
JULIA SHEN 1449, 1399, 1242, 1332, 1229 1330
JEZZEL FARKAS 1441, 1384, 1270, 1362, 1332, 1522, 980 1327
ASHWIN BALAJI 1186 1186
THOMAS JOSEPH HOSMER 1553, 1382, 1291, 1283, 1242 1350
BEN LI 1555, 1522, 1494, 1291, 377, 1332, 1270 1263
#print(output[, c("Player", "OpponentPreRatings", "AvgOpponentRating")])

Plot top 5 and bottom 5 players based on Average Opponent Rating.

# Get top 5 and bottom 5 by AvgOpponentRating
top5 <- output[order(-output$AvgOpponentRating), ][1:5, ]
bottom5 <- output[order(output$AvgOpponentRating), ][1:5, ]

# Combine top and bottom
subset_output <- rbind(top5, bottom5)

# Reorder factor levels for plotting
subset_output$Player <- factor(subset_output$Player, levels = subset_output$Player[order(subset_output$AvgOpponentRating)])


# Plot
ggplot(subset_output, aes(x = Player, y = AvgOpponentRating)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  geom_text(aes(label = PreRating), 
            hjust = 1.1,    # move text inside bar, aligned right
            size = 3.5, 
            color = "white") +  # white text inside bar
  coord_flip() +  # Horizontal bars
  labs(
    title = "Top 5 and Bottom 5 Players by Average Opponent Rating",
    x = "Player",
    y = "Average Opponent Rating"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.y = element_text(size = 9),
    plot.title = element_text(size = 14, face = "bold")
  ) +
  ylim(0, max(subset_output$AvgOpponentRating, na.rm = TRUE) + 100)

Gary Hua faced the strongest opponents, with an average opponent rating of 1605, followed closely by Patrick H. Schilling, who also competed against highly rated players. Both of these players encountered consistently tough competition.

In contrast, Derek Yan, Robert Glen Vasey, and Jared Ge faced weaker opponents on average, with much lower ratings compared to the top group.

This highlights an important insight: a strong player (high PreRating) might have an easier path if matched against weaker opponents, while a lower-rated player could face tougher challenges if paired against stronger opponents. This makes Average Opponent Rating a valuable measure of the difficulty level each player experienced.

For example, Anvit Rao entered with a PreRating of 1365, which is relatively low compared to others in the top group. Yet, his Average Opponent Rating of about 1554 shows that he was consistently paired against players who were significantly stronger than him on paper.