The goal of this project is to 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.
library(stringr)
## Import data from tournament info text file.
chesstour <- read.csv(paste0("https://raw.githubusercontent.com/SieSiongWong/DATA-607/master/tournamentinfo.txt"), header=FALSE)
## Remove header rows.
chesstour <- chesstour[-c(1:4),]
## Checking if header rows 1 to 4 removed successfully.
head(chesstour)
## [1] 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4|
## [2] ON | 15445895 / R: 1794 ->1817 |N:2 |W |B |W |B |W |B |W |
## [3] -----------------------------------------------------------------------------------------
## [4] 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7|
## [5] MI | 14598900 / R: 1553 ->1663 |N:2 |B |W |B |W |B |W |B |
## [6] -----------------------------------------------------------------------------------------
## 131 Levels: ----------------------------------------------------------------------------------------- ...
## Select sequential pieces for each player info and its rating into different data frames.
PlyInfo <- chesstour[seq(1,length(chesstour),3)]
RtiInfo <- chesstour[seq(2,length(chesstour),3)]
## Further extract required elements for this project.
#### Extract player's number and convert its character type to integer.
Player_Number <- unlist(str_extract(PlyInfo,"[[:digit:]]{1,2}"))
Player_Number <- as.integer(Player_Number)
#### Extract player's name.
Player_Name <- unlist(str_extract(PlyInfo,"[[:alpha:]]{2,}.{1,}([[:alpha:]]{2,})"))
#### Extract player's points and convert its character type to numeric.
Player_Points <- unlist(str_extract(PlyInfo,"[[:digit:]]{1}\\.[[:digit:]]{1}"))
Player_Points <- as.numeric(Player_Points)
#### Extract player's state.
Player_State <- unlist(str_extract(RtiInfo,"[[:alpha:]]{1,2}"))
#### Extract player's pre-rating and using sub function to find and remove ": " and then convert to numeric.
Player_Rating <- unlist(str_extract(RtiInfo, ":\\s{1,}[[:digit:]]{3,4}"))
Player_Rating <- sub(":\\s{1,}","",Player_Rating)
Player_Rating <- as.numeric(Player_Rating)
#### Extract each player's opponents' number.
Opponents_Number <- str_extract_all(PlyInfo, "[[:digit:]]{1,}\\|")
Opponents_Number <- str_extract_all(Opponents_Number, "[[:digit:]]{1,}")
#### Calculate the sum of Win, Lose, and Draw counts for each player.
Player_GamesCount <- str_count(PlyInfo, "\\|W") + str_count(PlyInfo, "\\|L") + str_count(PlyInfo, "\\|D")
#### Calculate the sum of opponents' rating for each player.
Player_TotalRating <- length(Player_Number)
for (i in 1:length(Player_Number))
{
Player_TotalRating[i] <- sum(Player_Rating[as.numeric(unlist(Opponents_Number[Player_Number[i]]))])
}
#### Calculate the average pre chess rating of opponents for each player.
Player_AverageRating <- round(Player_TotalRating/Player_GamesCount,0)
## Combine above data frames.
chesstour_summary <- cbind.data.frame(Player_Name,Player_State,Player_Points,Player_Rating, Player_AverageRating)
## Update the columns' names.
colnames(chesstour_summary) <- c("Player’s Name", "Player’s State", "Total Number of Points", "Player’s Pre-Rating", "Average Pre Chess Rating of Opponents")
## Display few rows of the summary results.
head(chesstour_summary)
## Player’s Name Player’s State Total Number of Points
## 1 GARY HUA ON 6.0
## 2 DAKSHESH DARURI MI 6.0
## 3 ADITYA BAJAJ MI 6.0
## 4 PATRICK H SCHILLING MI 5.5
## 5 HANSHI ZUO MI 5.5
## 6 HANSEN SONG OH 5.0
## Player’s Pre-Rating Average Pre Chess Rating of Opponents
## 1 1794 1605
## 2 1553 1469
## 3 1384 1564
## 4 1716 1574
## 5 1655 1501
## 6 1686 1519
## Export the summary to a CSV file.
write.csv(chesstour_summary,'ChesstourSummary.csv')