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:
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
1605 was calculated by using the pre-tournament opponents’ ratings of 1436, 1563, 1600, 1610, 1649, 1663, 1716, and dividing by the total number of games played.
# Load library
library(stringr)
# Read data file
chess_tournament <- read.csv("https://raw.githubusercontent.com/saayedalam/Data/master/tournamentinfo.txt", skip = 3)
# Separate two rows of the same instance
top_row <- seq(1, nrow(chess_tournament), 3)
bottom_row <- seq(2, nrow(chess_tournament), 3)
# Extract player's name
name <- str_extract_all(unlist(chess_tournament[top_row, ]), "\\w+(\\ \\w+ (\\w|-)* \\w*)")
name <- str_trim(unlist(name))
# Extract player's state
state <- str_extract_all(unlist(chess_tournament[bottom_row, ]), "[[:alpha:]]{2}")
state <- str_trim(unlist(state))
# Extract total number of points
total_points <- str_extract_all(unlist(chess_tournament[top_row, ]), "\\d(\\.\\d)")
total_points <- str_trim(unlist(total_points))
# Extract player's pre-rating
pre_rating <- str_extract_all(unlist(chess_tournament[bottom_row, ]),"R: [[:digit:] ]*")
pre_rating <- str_trim(unlist(pre_rating))
pre_rating <- str_replace_all(pre_rating, "R: ", "")
pre_rating <- as.numeric(pre_rating)
# Extract opponent's ID
opponent <- str_extract_all(chess_tournament[top_row, 1], "\\d{1,2}(?=\\|)")
# Average pre chess rating of opponents
average_rating_opponents <- sapply(opponent, function(x) mean(pre_rating[as.numeric(x)]))
chess_tournament_result <- data.frame( "Name" = name, "State" = state, "Total Points" = total_points, "Pre-Rating"
= pre_rating, "Opponents Pre-Rating" = round(average_rating_opponents))
head(chess_tournament_result, 10)
## Name State Total.Points Pre.Rating Opponents.Pre.Rating
## 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
# Generate .CSV file
write.csv(chess_tournament_result, file="Chess Tournament (Project 1).csv", row.names = FALSE)