Question:

Use the provided 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


  1. First, import chess data from the txt file into R:
score_card = readLines("https://raw.githubusercontent.com/isrini/SI_IS607/master/tournamentinfo.txt");
  1. Using the stringr library to extract the data:
library(stringr);
library(knitr);
  1. Thw txt file has two rows of data for each player. Lets extract each row separately and remove the blanks - space and tab:
row1 = unlist(str_extract_all(score_card,"^[[:blank:]]+\\d{1,2}.+"))
row2 = unlist(str_extract_all(score_card,"^[[:blank:]]+[A-Z]{2}.+"))
head(row1);
## [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|"
head(row2);
## [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    |"
  1. And extract the chess player name from row 1, the state they represent from row 2 and total points from row 1
# names are in upper case
player_name = unlist(str_extract_all(row1,"(\\b[[:upper:]-]+\\b\\s)+(\\b[[:upper:]-]+\\b){1}"))

# state name is also in upper case
state_name = unlist(str_extract_all(row2,"[[:upper:]]{2}" ))

# points are decimal numbers
points = as.numeric(unlist(str_extract_all(row1,"\\d(.)\\d")))                                  
  1. Now lets extract the pre rating data from row 2:
# some of the ratings have a 'P' followedd by additional numbers, we do not need this info, replace it
pre_rating <- str_replace_all(row2,pattern="[P]\\d{1,}"," ")

# removing the prefix of R: from the pre_rating
r_colon <- str_extract_all(pre_rating,"([R(:)][[:blank:]]+\\d{3,}+)") 

# the pre rating and converting it to a numeric
pre_rating <- as.numeric(str_extract_all(r_colon,"\\d{3,}"))          
  1. Lets put all these together in a data frame:
score_df <- data.frame(player_name, state_name, points, pre_rating);
head(score_df);
##           player_name state_name points pre_rating
## 1            GARY HUA         ON    6.0       1794
## 2     DAKSHESH DARURI         MI    6.0       1553
## 3        ADITYA BAJAJ         MI    6.0       1384
## 4 PATRICK H SCHILLING         MI    5.5       1716
## 5          HANSHI ZUO         MI    5.5       1655
## 6         HANSEN SONG         OH    5.0       1686
  1. Now lets get the opponent player numbers for each of the 64 players from row 1:
# each player number has a prefix of W,L or D followed by a space, we do not need this
player_opponents <- str_extract_all(row1,"[WLD][[:blank:]]+\\d{1,2}") 

Opponents <-str_extract_all(player_opponents,"\\d{1,2}")

# convert the player numbers to numeric
opponents <- lapply(Opponents, as.numeric)                            
  1. We need to calculate the average pre chess rating for each player based on their opponents pre_rating numbers. Lets create a function to calculate the average rating using their opponents information from above.
avg_opp_rate <- function(a){
  
  rating <- 0
  no_of_opponents <- length(a)

  for (i in a){
    rating <- rating + score_df[i,"pre_rating"]}
    return(rating / no_of_opponents)
  }

avg_pre_chess_rating <- unlist(lapply(opponents, avg_opp_rate));

# convert to numeric as well as rounding off the ranking
avg_pre_chess_rating <- round(avg_pre_chess_rating, 0)
  1. And bringing it all together now, in to a data frame which includes the required columns to answer the question above.
score_df <- data.frame(player_name, state_name, points, pre_rating, avg_pre_chess_rating);
kable(score_df)
player_name state_name points pre_rating avg_pre_chess_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 LEMAN 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 STANESCU-BELLU 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 CORMICK 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

  1. Writing the results in to a csv file
write.csv(score_df, file ="SI_project1_results.csv")