Given a text file with chess tournament results, create an R Markdown file that generates a .CSV file with the following information for all of the players:
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
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
chess_lines <- read_lines("https://raw.githubusercontent.com/gsteinmetzsilber/DATA607/project_1/Project1_Data.txt")
head(chess_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|"
Every third row is a series of hyphens, and all such rows are not helpful for our purposes.
hyphen_rows <- seq(1, 196, 3)
chess_lines <- chess_lines[-hyphen_rows]
Additionally, each observation currently takes up two rows:
From name rows, we can derive: Name, Total Points, Average Pre-Chess Rating of Opponents
From non-name rows, we can derive: State, Player’s Pre-Rating
To simplify the work later on, I’ll divide chess_lines into two:
seq_name_rows <- seq(3, 130, 2)
chess_name_rows <- chess_lines[seq_name_rows]
seq_nonname_rows <- seq(4, 130, 2)
chess_nonname_rows <- chess_lines[seq_nonname_rows]
length(chess_name_rows) #confirming there are 64 name rows
## [1] 64
length(chess_nonname_rows) #confirming there are 64 non-name rows
## [1] 64
head(chess_name_rows, 30)
## [1] " 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4|"
## [2] " 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7|"
## [3] " 3 | ADITYA BAJAJ |6.0 |L 8|W 61|W 25|W 21|W 11|W 13|W 12|"
## [4] " 4 | PATRICK H SCHILLING |5.5 |W 23|D 28|W 2|W 26|D 5|W 19|D 1|"
## [5] " 5 | HANSHI ZUO |5.5 |W 45|W 37|D 12|D 13|D 4|W 14|W 17|"
## [6] " 6 | HANSEN SONG |5.0 |W 34|D 29|L 11|W 35|D 10|W 27|W 21|"
## [7] " 7 | GARY DEE SWATHELL |5.0 |W 57|W 46|W 13|W 11|L 1|W 9|L 2|"
## [8] " 8 | EZEKIEL HOUGHTON |5.0 |W 3|W 32|L 14|L 9|W 47|W 28|W 19|"
## [9] " 9 | STEFANO LEE |5.0 |W 25|L 18|W 59|W 8|W 26|L 7|W 20|"
## [10] " 10 | ANVIT RAO |5.0 |D 16|L 19|W 55|W 31|D 6|W 25|W 18|"
## [11] " 11 | CAMERON WILLIAM MC LEMAN |4.5 |D 38|W 56|W 6|L 7|L 3|W 34|W 26|"
## [12] " 12 | KENNETH J TACK |4.5 |W 42|W 33|D 5|W 38|H |D 1|L 3|"
## [13] " 13 | TORRANCE HENRY JR |4.5 |W 36|W 27|L 7|D 5|W 33|L 3|W 32|"
## [14] " 14 | BRADLEY SHAW |4.5 |W 54|W 44|W 8|L 1|D 27|L 5|W 31|"
## [15] " 15 | ZACHARY JAMES HOUGHTON |4.5 |D 19|L 16|W 30|L 22|W 54|W 33|W 38|"
## [16] " 16 | MIKE NIKITIN |4.0 |D 10|W 15|H |W 39|L 2|W 36|U |"
## [17] " 17 | RONALD GRZEGORCZYK |4.0 |W 48|W 41|L 26|L 2|W 23|W 22|L 5|"
## [18] " 18 | DAVID SUNDEEN |4.0 |W 47|W 9|L 1|W 32|L 19|W 38|L 10|"
## [19] " 19 | DIPANKAR ROY |4.0 |D 15|W 10|W 52|D 28|W 18|L 4|L 8|"
## [20] " 20 | JASON ZHENG |4.0 |L 40|W 49|W 23|W 41|W 28|L 2|L 9|"
## [21] " 21 | DINH DANG BUI |4.0 |W 43|L 1|W 47|L 3|W 40|W 39|L 6|"
## [22] " 22 | EUGENE L MCCLURE |4.0 |W 64|D 52|L 28|W 15|H |L 17|W 40|"
## [23] " 23 | ALAN BUI |4.0 |L 4|W 43|L 20|W 58|L 17|W 37|W 46|"
## [24] " 24 | MICHAEL R ALDRICH |4.0 |L 28|L 47|W 43|L 25|W 60|W 44|W 39|"
## [25] " 25 | LOREN SCHWIEBERT |3.5 |L 9|W 53|L 3|W 24|D 34|L 10|W 47|"
## [26] " 26 | MAX ZHU |3.5 |W 49|W 40|W 17|L 4|L 9|D 32|L 11|"
## [27] " 27 | GAURAV GIDWANI |3.5 |W 51|L 13|W 46|W 37|D 14|L 6|U |"
## [28] " 28 | SOFIA ADINA STANESCU-BELLU |3.5 |W 24|D 4|W 22|D 19|L 20|L 8|D 36|"
## [29] " 29 | CHIEDOZIE OKORIE |3.5 |W 50|D 6|L 38|L 34|W 52|W 48|U |"
## [30] " 30 | GEORGE AVERY JONES |3.5 |L 52|D 64|L 15|W 55|L 31|W 61|W 50|"
From this sample, I can see two things that will influence my Regex pattern:
There’s only one vertical bar followed by a space, and that’s the vertical bar before the names.
Players have at least two names, but some have three or four. One player has three names, and the final name is hyphenated.
pattern_names <- "(?<=[|]\\s)[A-Z]{0,30}\\s[A-Z]{0,30}(\\s[A-Z]{0,30}[-]?\\s?[A-Z]{0,30})?"
names <-str_extract(chess_name_rows, pattern_names)
names <- str_trim(names) #removing the extra whitespace
names <- str_to_title(names) #fixing the capitalization
chess_df <-data.frame(Name = names)
head(chess_df)
head(chess_nonname_rows, 10)
## [1] " ON | 15445895 / R: 1794 ->1817 |N:2 |W |B |W |B |W |B |W |"
## [2] " MI | 14598900 / R: 1553 ->1663 |N:2 |B |W |B |W |B |W |B |"
## [3] " MI | 14959604 / R: 1384 ->1640 |N:2 |W |B |W |B |W |B |W |"
## [4] " MI | 12616049 / R: 1716 ->1744 |N:2 |W |B |W |B |W |B |B |"
## [5] " MI | 14601533 / R: 1655 ->1690 |N:2 |B |W |B |W |B |W |B |"
## [6] " OH | 15055204 / R: 1686 ->1687 |N:3 |W |B |W |B |B |W |B |"
## [7] " MI | 11146376 / R: 1649 ->1673 |N:3 |W |B |W |B |B |W |W |"
## [8] " MI | 15142253 / R: 1641P17->1657P24 |N:3 |B |W |B |W |B |W |W |"
## [9] " ON | 14954524 / R: 1411 ->1564 |N:2 |W |B |W |B |W |B |B |"
## [10] " MI | 14150362 / R: 1365 ->1544 |N:3 |W |W |B |B |W |B |W |"
Again, two observations:
States are the only text immediately preceding a space then a vertical bar.
States are two letters
pattern_states <- "[A-Z][A-Z](?=\\s[|])"
states <- str_extract(chess_nonname_rows, pattern_states)
chess_df$State <- states
head(chess_df)
The total points can be found in the name rows which I previewed earlier. The total points are the only part of these rows with a decimal and I’ll use that to inform my pattern.
pattern_points <- "\\d[.]\\d"
points <- str_extract(chess_name_rows, pattern_points)
chess_df$Total_Points <- points
chess_df$Total_Points <- as.numeric(chess_df$Total_Points)
head(chess_df)
Ratings are in the non-name rows. There is inconsistency in the spacing around the ratings, so to simplify, I’ll remove all whitespace from those rows. Once that’s done:
Ratings will immediately be followed by an arrow
Ratings are three or four digits. Some ratings then have a “P” and one or two digits.
chess_nonname_rows_nospace <- gsub("\\s", "", chess_nonname_rows)
pattern_ratings <- "(\\d\\d\\d)(\\d)?(P\\d(\\d)?)?(?=[-][>])"
ratings <- str_extract(chess_nonname_rows_nospace, pattern_ratings)
ratings <- gsub("P", ".", ratings, fixed = TRUE) #replacing P with a decimal point
chess_df$Pre_Rating <- ratings
chess_df$Pre_Rating <- as.numeric(chess_df$Pre_Rating)
chess_df$Pre_Rating <- round(chess_df$Pre_Rating)
head(chess_df)
This average is not in any row, however the indices of all opponents are in chess_name_rows. As was previously the case, the spacing around these indices is a bit inconsistent. I’ll remove whitespace to simplify matters. When that’s complete:
Opponent indices (and only opponent indices) will immediately follow a “W,” “L,” or “D.”
Opponent indices are either one or two digits (there are a total of 64 players).
chess_name_rows_nospace <- gsub("\\s", "", chess_name_rows)
pattern_opponents <- "(?<=[DWL])\\d(\\d)?"
opponents <- str_extract_all(chess_name_rows_nospace, pattern_opponents)
Each line now has the list of opponents (by index) . We have the opponents’ ratings in chess_df, so we can replace all the indices with the ratings of the corresponding players, average those ratings, and add them to the dataframe.
opponents <- lapply(opponents, as.numeric) #converting indices to "numeric"
opponents_ratings <- lapply(opponents, function(index) chess_df$Pre_Rating[index])
opponents_ratings_avg <- lapply(opponents_ratings, mean)
opponents_ratings_avg <- unlist(opponents_ratings_avg)
chess_df$Opponents_Average_Pre_Rating <- opponents_ratings_avg
chess_df$Opponents_Average_Pre_Rating <- round(chess_df$Opponents_Average_Pre_Rating) #rounding
head(chess_df)
Now that the dataframe is in good shape, I can generate the .CSV file:
write.csv(chess_df, "chess_tournament_results.csv")