Load the Tournament Data

tournament <- readLines(
  "https://raw.githubusercontent.com/mj-nurse/mnurse-data_607/refs/heads/main/project-01-chess-tournament/tournamentinfo.txt"
)
## Warning in
## readLines("https://raw.githubusercontent.com/mj-nurse/mnurse-data_607/refs/heads/main/project-01-chess-tournament/tournamentinfo.txt"):
## incomplete final line found on
## 'https://raw.githubusercontent.com/mj-nurse/mnurse-data_607/refs/heads/main/project-01-chess-tournament/tournamentinfo.txt'
head(tournament, 10)
##  [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] "-----------------------------------------------------------------------------------------"

Extract Player Information

# Number of players in the tournament
n_players <- 64

# Create an empty data frame for the player information
players <- data.frame(
  Pair_Num = integer(n_players),
  Player_Name = character(n_players),
  Player_State = character(n_players),
  Total_Points = numeric(n_players),
  Pre_Rating = integer(n_players)
)

# Create an empty matrix for the 7 opponents
opponents <- matrix(
  NA_integer_,
  nrow = n_players,
  ncol = 7
)

colnames(opponents) <- paste0("Round_", 1:7)

# Go through each player
for (i in 1:n_players) {

  # Each player has two lines followed by a separator
  player_line <- tournament[5 + (i - 1) * 3]
  detail_line <- tournament[6 + (i - 1) * 3]

  # Split the lines at each |
  player_parts <- strsplit(player_line, "|", fixed = TRUE)[[1]]
  detail_parts <- strsplit(detail_line, "|", fixed = TRUE)[[1]]

  # Extract basic player information
  players$Pair_Num[i] <- as.integer(trimws(player_parts[1]))
  players$Player_Name[i] <- trimws(player_parts[2])
  players$Total_Points[i] <- as.numeric(trimws(player_parts[3]))
  players$Player_State[i] <- trimws(detail_parts[1])

  # Extract the pre-tournament rating
  rating <- strsplit(detail_parts[2], "R:", fixed = TRUE)[[1]][2]
  rating <- strsplit(rating, "->", fixed = TRUE)[[1]][1]
  rating <- strsplit(trimws(rating), "P", fixed = TRUE)[[1]][1]

  players$Pre_Rating[i] <- as.integer(trimws(rating))

  # Extract opponent numbers from each round
  for (round in 1:7) {

    round_text <- trimws(player_parts[3 + round])

    if (nchar(round_text) > 1) {

      opponent_number <- trimws(
        substr(round_text, 2, nchar(round_text))
      )

      opponents[i, round] <- as.integer(opponent_number)

    } else {

      opponents[i, round] <- NA
    }
  }
}

# Show the first few results
head(players)
##   Pair_Num         Player_Name Player_State Total_Points Pre_Rating
## 1        1            GARY HUA           ON          6.0       1794
## 2        2     DAKSHESH DARURI           MI          6.0       1553
## 3        3        ADITYA BAJAJ           MI          6.0       1384
## 4        4 PATRICK H SCHILLING           MI          5.5       1716
## 5        5          HANSHI ZUO           MI          5.5       1655
## 6        6         HANSEN SONG           OH          5.0       1686
head(opponents)
##      Round_1 Round_2 Round_3 Round_4 Round_5 Round_6 Round_7
## [1,]      39      21      18      14       7      12       4
## [2,]      63      58       4      17      16      20       7
## [3,]       8      61      25      21      11      13      12
## [4,]      23      28       2      26       5      19       1
## [5,]      45      37      12      13       4      14      17
## [6,]      34      29      11      35      10      27      21

Look Up Scores

# Create a new column for average opponent rating
players$Average_Opponent_Rating <- NA

# Go through each player
for (i in 1:n_players) {

  # Get this player's opponent numbers
  opponent_numbers <- opponents[i, ]

  # Remove rounds where there was no opponent
  opponent_numbers <- opponent_numbers[!is.na(opponent_numbers)]

  # Create a place to store the opponent ratings
  opponent_ratings <- numeric(length(opponent_numbers))

  # Look up each opponent's pre-tournament rating
  for (j in 1:length(opponent_numbers)) {

    opponent_row <- match(
      opponent_numbers[j],
      players$Pair_Num
    )

    opponent_ratings[j] <- players$Pre_Rating[opponent_row]
  }

  # Calculate the average opponent rating
  players$Average_Opponent_Rating[i] <- round(
    mean(opponent_ratings)
  )
}

# Keep only the columns required for the assignment
final_results <- players[, c(
  "Player_Name",
  "Player_State",
  "Total_Points",
  "Pre_Rating",
  "Average_Opponent_Rating"
)]

head(final_results)
##           Player_Name Player_State Total_Points 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_Opponent_Rating
## 1                    1605
## 2                    1469
## 3                    1564
## 4                    1574
## 5                    1501
## 6                    1519

Import to CSV

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

# Final checks
nrow(final_results)
## [1] 64
head(final_results)
##           Player_Name Player_State Total_Points 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_Opponent_Rating
## 1                    1605
## 2                    1469
## 3                    1564
## 4                    1574
## 5                    1501
## 6                    1519
tail(final_results)
##             Player_Name Player_State Total_Points Pre_Rating
## 59    SEAN M MC CORMICK           MI          2.0        853
## 60           JULIA SHEN           MI          1.5        967
## 61        JEZZEL FARKAS           ON          1.5        955
## 62        ASHWIN BALAJI           MI          1.0       1530
## 63 THOMAS JOSEPH HOSMER           MI          1.0       1175
## 64               BEN LI           MI          1.0       1163
##    Average_Opponent_Rating
## 59                    1319
## 60                    1330
## 61                    1327
## 62                    1186
## 63                    1350
## 64                    1263

Conclusion

The tournament text file was transformed into a structured dataset containing each player’s name, state, total points, pre-tournament rating, and average opponent pre-rating. The final dataset contains 64 players and was exported to a CSV file.