In this project, you’re given a text file with chess tournament results where the information has some structure. Your job is to create a R Markdown file that generates a .CSV file(that could for example be imported into a SQL database) with the following information for all players:
The first thing I did was create an excel spreadsheet that contained each players info along with their scores for each round. I used an excel workbook because I would be able to make updates in the Excel file and once I save, I can run the code again and it will reflect the updates.
NoW, we need to set the libraries that we will be utilizing for this project. I went back and added rJava and xlsx.
library(plyr)
library(stringr)
library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
library(rJava)
The code below will load our excel file. Next we want to manipulate the file a bit. We will rename the columns first.
df <- read.xlsx("Project 1.xlsx", 1)
chess.info <- df
head(chess.info)
## Player.Name Player.State Pre.Rating Round.1 Round.2 Round.3
## 1 Gary Hua ON 1794 1436 1563 1600
## 2 Dakshesh Daruri MI 1553 1175 917 1716
## 3 Aditya Bajaj MI 1384 1641 955 1745
## 4 Patrick Schilling MI 1716 1363 1507 1553
## 5 Hanshi Zuo MI 1655 1242 980 1663
## 6 Hansen Song OH 1686 1399 160 1712
## Round.4 Round.5 Round.5.1 Round.7 Total.Points Ave..Op..Rating
## 1 1610 1647 1663 1716 6.0 1605.000
## 2 1629 1604 1595 1649 6.0 1469.286
## 3 1563 1712 1666 1663 6.0 1563.571
## 4 1579 1655 1564 1794 5.5 1573.571
## 5 1666 1716 1610 1629 5.5 1500.857
## 6 1438 1365 1552 1563 5.0 1312.714
The columns will be renamed for a neater looking file.
chessinfo.name <-rename(chess.info, c("Player.Name"="Player Name", "Player.State"="State", "Pre.Rating"="Pre-Rating", "Round.1"="Round 1", "Round.2"="Round 2", "Round.3"="Round 3", "Round.4"="Round 4", "Round.5"="Round 5", "Round.5.1"="Round 6", "Round.7"="Round 7", "Total.Points"="Ttl. Pts.", "Ave..Op..Rating"="Ave.Opp.Rating"))
head(chessinfo.name)
## Player Name State Pre-Rating Round 1 Round 2 Round 3 Round 4
## 1 Gary Hua ON 1794 1436 1563 1600 1610
## 2 Dakshesh Daruri MI 1553 1175 917 1716 1629
## 3 Aditya Bajaj MI 1384 1641 955 1745 1563
## 4 Patrick Schilling MI 1716 1363 1507 1553 1579
## 5 Hanshi Zuo MI 1655 1242 980 1663 1666
## 6 Hansen Song OH 1686 1399 160 1712 1438
## Round 5 Round 6 Round 7 Ttl. Pts. Ave.Opp.Rating
## 1 1647 1663 1716 6.0 1605.000
## 2 1604 1595 1649 6.0 1469.286
## 3 1712 1666 1663 6.0 1563.571
## 4 1655 1564 1794 5.5 1573.571
## 5 1716 1610 1629 5.5 1500.857
## 6 1365 1552 1563 5.0 1312.714
After renaming, I would like to create a subset that contains just the columns we need for the CSV. We don’t need to include each opponent’s pre-rating.
chess.tournament <- chessinfo.name[, c(1,2,3,11,12)]
head(chess.tournament)
## Player Name State Pre-Rating Ttl. Pts. Ave.Opp.Rating
## 1 Gary Hua ON 1794 6.0 1605.000
## 2 Dakshesh Daruri MI 1553 6.0 1469.286
## 3 Aditya Bajaj MI 1384 6.0 1563.571
## 4 Patrick Schilling MI 1716 5.5 1573.571
## 5 Hanshi Zuo MI 1655 5.5 1500.857
## 6 Hansen Song OH 1686 5.0 1312.714
Finally, to create the .CSV file.
write.csv(chess.tournament, file='chess_final.csv')