Introduction

After submitting Project 1, I was informed that my submission relied too heavily on Excel due to the fact that I completed the calculations in Excel. Fortunately I was given an opportunity to re-do Project 1 after getting the chance to take a look at other students’ completed work.

For this project, we are given a text file with chess tournament results where the information has some structure. We are responsible for creating an R Markdown file that generates a .csv with the following information for all the players:
  • Player’s Name
  • Player’s State
  • Total Number of Points
  • Player’s Pre-Rating
  • Average Pre-Chess Rating of Opponents
We need to calculate the Average Pre Chess Rating of Opponents by adding the pre-rating of each opponent and dividing it by the number of matches played.
Load libraries that are required:
  • ‘stringr’: we will be using the ‘stringr’ package for manipulating the data
library(stringr)
Read in the text file:
chessinfo <- readLines('chessinfo.txt', warn = FALSE)  
#I get a warning message when this line runs so I added the warn component to eliminate the warning. 
head(chessinfo)
## [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    |"
Once the file has been loaded, we will start manipulating our data by extracting all the pertinent information such as player’s names, state and opponent information using str_extract_all().
chessinfo <- chessinfo[-c(1:4)]
l <- length(chessinfo)
row.one <- as.factor(chessinfo[seq(1,l,3)])
#we have to create a subset that contains the names

head(row.one)
## [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|
## 64 Levels:     1 | GARY HUA                        |6.0  |W  39|W  21|W  18|W  14|W   7|D  12|D   4| ...
row.two <- as.factor(chessinfo[seq(2,l,3)])   
#this will contain all the alternate row information, basically just dividing up by every other row

head(row.two)
## [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    |
## 64 Levels:    MI | 10131499 / R: 1610   ->1618     |N:3  |W    |B    |W    |W    |B    |B    |W    | ...
player.name <- str_trim(str_extract(row.one, "(\\w+\\s){2,3}"))  
#this will extract all words with 2 or more letters, with a space and up to 3 words and then it str_trim will trim down to a new string.

head(player.name)
## [1] "GARY HUA"            "DAKSHESH DARURI"     "ADITYA BAJAJ"       
## [4] "PATRICK H SCHILLING" "HANSHI ZUO"          "HANSEN SONG"
scores <- as.double(unlist(str_extract_all(row.one, "[:digit:][.][:digit:]")))  
#this will extract all numbers in the format x.x and sets them as doubles 

head(scores)
## [1] 6.0 6.0 6.0 5.5 5.5 5.0
opponent <- str_extract_all(row.one, "[:digit:]+?\\|") #this is extract all digits followed by |
opponent <- str_extract_all(opponent, "\\d+")
head(opponent)
## [[1]]
## [1] "39" "21" "18" "14" "7"  "12" "4" 
## 
## [[2]]
## [1] "63" "58" "4"  "17" "16" "20" "7" 
## 
## [[3]]
## [1] "8"  "61" "25" "21" "11" "13" "12"
## 
## [[4]]
## [1] "23" "28" "2"  "26" "5"  "19" "1" 
## 
## [[5]]
## [1] "45" "37" "12" "13" "4"  "14" "17"
## 
## [[6]]
## [1] "34" "29" "11" "35" "10" "27" "21"
states <- str_extract(row.two, "\\w+")
head(states)
## [1] "ON" "MI" "MI" "MI" "MI" "OH"
pre.rating <- str_extract(row.two, "[^[:digit:]][:digit:]{1,4}[^[:digit:]]")
pre.rating <- str_extract(pre.rating, "\\d+")
#I initially tried code that would extract all four digit numbers with spaces before and after but that didn't take into account elements such as 955P11. This approach worked much better

head(pre.rating)
## [1] "1794" "1553" "1384" "1716" "1655" "1686"
Now that we have the names, states, scores, and pre-rating extracted, we need to calculated the average opponent pre-rating. We will use a loop to do this.
average.preRating <- 0

for (i in 1:length(player.name)){
  average.preRating[i] <- round(mean(as.numeric(pre.rating[as.numeric(unlist(opponent[i]))]), na.rm = TRUE, digits = 0))
}
head(average.preRating)
## [1] 1605 1469 1564 1574 1501 1519
Finally, I will combine all the parts into a data frame, rename the columns and convert to a table.
chess.output <- data.frame(player.name, states, scores, pre.rating, average.preRating)
names(chess.output) <- c("Player's Name","State", "Score", "Pre Rating","Ave.Opp.Pre Rating")
chess.output  
##               Player's Name State Score Pre Rating Ave.Opp.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
## 11       CAMERON WILLIAM MC    MI   4.5       1712               1468
## 12           KENNETH J TACK    MI   4.5       1663               1506
## 13        TORRANCE HENRY JR    MI   4.5       1666               1498
## 14             BRADLEY SHAW    MI   4.5       1610               1515
## 15   ZACHARY JAMES HOUGHTON    MI   4.5       1220               1484
## 16             MIKE NIKITIN    MI   4.0       1604               1386
## 17       RONALD GRZEGORCZYK    MI   4.0       1629               1499
## 18            DAVID SUNDEEN    MI   4.0       1600               1480
## 19             DIPANKAR ROY    MI   4.0       1564               1426
## 20              JASON ZHENG    MI   4.0       1595               1411
## 21            DINH DANG BUI    ON   4.0       1563               1470
## 22         EUGENE L MCCLURE    MI   4.0       1555               1300
## 23                 ALAN BUI    ON   4.0       1363               1214
## 24        MICHAEL R ALDRICH    MI   4.0       1229               1357
## 25         LOREN SCHWIEBERT    MI   3.5       1745               1363
## 26                  MAX ZHU    ON   3.5       1579               1507
## 27           GAURAV GIDWANI    MI   3.5       1552               1222
## 28              SOFIA ADINA    MI   3.5       1507               1522
## 29         CHIEDOZIE OKORIE    MI   3.5       1602               1314
## 30       GEORGE AVERY JONES    ON   3.5       1522               1144
## 31             RISHI SHETTY    MI   3.5       1494               1260
## 32    JOSHUA PHILIP MATHEWS    ON   3.5       1441               1379
## 33                  JADE GE    MI   3.5       1449               1277
## 34   MICHAEL JEFFERY THOMAS    MI   3.5       1399               1375
## 35         JOSHUA DAVID LEE    MI   3.5       1438               1150
## 36            SIDDHARTH JHA    MI   3.5       1355               1388
## 37     AMIYATOSH PWNANANDAM    MI   3.5        980               1385
## 38                BRIAN LIU    MI   3.0       1423               1539
## 39            JOEL R HENDON    MI   3.0       1436               1430
## 40             FOREST ZHANG    MI   3.0       1348               1391
## 41      KYLE WILLIAM MURPHY    MI   3.0       1403               1248
## 42                 JARED GE    MI   3.0       1332               1150
## 43        ROBERT GLEN VASEY    MI   3.0       1283               1107
## 44       JUSTIN D SCHILLING    MI   3.0       1199               1327
## 45                DEREK YAN    MI   3.0       1242               1152
## 46 JACOB ALEXANDER LAVALLEY    MI   3.0        377               1358
## 47              ERIC WRIGHT    MI   2.5       1362               1392
## 48             DANIEL KHAIN    MI   2.5       1382               1356
## 49         MICHAEL J MARTIN    MI   2.5       1291               1286
## 50               SHIVAM JHA    MI   2.5       1056               1296
## 51           TEJAS AYYAGARI    MI   2.5       1011               1356
## 52                ETHAN GUO    MI   2.5        935               1495
## 53            JOSE C YBARRA    MI   2.0       1393               1345
## 54              LARRY HODGE    MI   2.0       1270               1206
## 55                ALEX KONG    MI   2.0       1186               1406
## 56             MARISA RICCI    MI   2.0       1153               1414
## 57               MICHAEL LU    MI   2.0       1092               1363
## 58             VIRAJ MOHILE    MI   2.0        917               1391
## 59                SEAN M MC    MI   2.0        853               1319
## 60               JULIA SHEN    MI   1.5        967               1330
## 61            JEZZEL FARKAS    ON   1.5        955               1327
## 62            ASHWIN BALAJI    MI   1.0       1530               1186
## 63     THOMAS JOSEPH HOSMER    MI   1.0       1175               1350
## 64                   BEN LI    MI   1.0       1163               1263
knitr::kable(chess.output)
Player’s Name State Score Pre Rating Ave.Opp.Pre Rating
GARY HUA ON 6.0 1794 1605
DAKSHESH DARURI MI 6.0 1553 1469
ADITYA BAJAJ MI 6.0 1384 1564
PATRICK H SCHILLING MI 5.5 1716 1574
HANSHI ZUO MI 5.5 1655 1501
HANSEN SONG OH 5.0 1686 1519
GARY DEE SWATHELL MI 5.0 1649 1372
EZEKIEL HOUGHTON MI 5.0 1641 1468
STEFANO LEE ON 5.0 1411 1523
ANVIT RAO MI 5.0 1365 1554
CAMERON WILLIAM MC MI 4.5 1712 1468
KENNETH J TACK MI 4.5 1663 1506
TORRANCE HENRY JR MI 4.5 1666 1498
BRADLEY SHAW MI 4.5 1610 1515
ZACHARY JAMES HOUGHTON MI 4.5 1220 1484
MIKE NIKITIN MI 4.0 1604 1386
RONALD GRZEGORCZYK MI 4.0 1629 1499
DAVID SUNDEEN MI 4.0 1600 1480
DIPANKAR ROY MI 4.0 1564 1426
JASON ZHENG MI 4.0 1595 1411
DINH DANG BUI ON 4.0 1563 1470
EUGENE L MCCLURE MI 4.0 1555 1300
ALAN BUI ON 4.0 1363 1214
MICHAEL R ALDRICH MI 4.0 1229 1357
LOREN SCHWIEBERT MI 3.5 1745 1363
MAX ZHU ON 3.5 1579 1507
GAURAV GIDWANI MI 3.5 1552 1222
SOFIA ADINA MI 3.5 1507 1522
CHIEDOZIE OKORIE MI 3.5 1602 1314
GEORGE AVERY JONES ON 3.5 1522 1144
RISHI SHETTY MI 3.5 1494 1260
JOSHUA PHILIP MATHEWS ON 3.5 1441 1379
JADE GE MI 3.5 1449 1277
MICHAEL JEFFERY THOMAS MI 3.5 1399 1375
JOSHUA DAVID LEE MI 3.5 1438 1150
SIDDHARTH JHA MI 3.5 1355 1388
AMIYATOSH PWNANANDAM MI 3.5 980 1385
BRIAN LIU MI 3.0 1423 1539
JOEL R HENDON MI 3.0 1436 1430
FOREST ZHANG MI 3.0 1348 1391
KYLE WILLIAM MURPHY MI 3.0 1403 1248
JARED GE MI 3.0 1332 1150
ROBERT GLEN VASEY MI 3.0 1283 1107
JUSTIN D SCHILLING MI 3.0 1199 1327
DEREK YAN MI 3.0 1242 1152
JACOB ALEXANDER LAVALLEY MI 3.0 377 1358
ERIC WRIGHT MI 2.5 1362 1392
DANIEL KHAIN MI 2.5 1382 1356
MICHAEL J MARTIN MI 2.5 1291 1286
SHIVAM JHA MI 2.5 1056 1296
TEJAS AYYAGARI MI 2.5 1011 1356
ETHAN GUO MI 2.5 935 1495
JOSE C YBARRA MI 2.0 1393 1345
LARRY HODGE MI 2.0 1270 1206
ALEX KONG MI 2.0 1186 1406
MARISA RICCI MI 2.0 1153 1414
MICHAEL LU MI 2.0 1092 1363
VIRAJ MOHILE MI 2.0 917 1391
SEAN M MC MI 2.0 853 1319
JULIA SHEN MI 1.5 967 1330
JEZZEL FARKAS ON 1.5 955 1327
ASHWIN BALAJI MI 1.0 1530 1186
THOMAS JOSEPH HOSMER MI 1.0 1175 1350
BEN LI MI 1.0 1163 1263
Now that my data our data is neat and contains our necessary information, we can write a .csv file.
write.csv(chess.output, file = "chess_output.csv")