Start by getting the raw data from github
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.1 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
#Get the raw data
source = 'https://raw.githubusercontent.com/BDavidoff/Data_607/master/Chess_Data.txt'
source_data = readLines(source, warn=FALSE)
length(source_data)
## [1] 196
Do some heavy handed trimming to remove the obviously bad parts of the text file
#cut the top 3 lines
tmp_data = source_data[-c(1,2,3)]
#remove every line that starts with a '-' character
tmp_data = tmp_data[!grepl("^-", tmp_data)]
#Get the top lines
top_lines = tmp_data[seq(1, length(tmp_data), 2)]
#Get the bottom lines
bot_lines = tmp_data[-c(1)]
bot_lines = bot_lines[seq(1, length(bot_lines), 2)]
#merge both lists together with a map
merged_data = paste0(top_lines, bot_lines)
final_data = Map({function(x) strsplit(x, split='\\|') }, merged_data)
Convert list to data frame
dt = data.frame(matrix(unlist(final_data), nrow=length(final_data), byrow=T))
#change the name of the columns for readability
dt = select(dt, 1:12)
colnames(dt) = c("ID", "Name", "Total Points", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "State", "Rating")
glimpse(dt)
## Rows: 64
## Columns: 12
## $ ID <chr> " 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " ...
## $ Name <chr> " GARY HUA ", " DAKSHESH DARU...
## $ `Total Points` <chr> "6.0 ", "6.0 ", "6.0 ", "5.5 ", "5.5 ", "5.0 "...
## $ R1 <chr> "W 39", "W 63", "L 8", "W 23", "W 45", "W 34"...
## $ R2 <chr> "W 21", "W 58", "W 61", "D 28", "W 37", "D 29"...
## $ R3 <chr> "W 18", "L 4", "W 25", "W 2", "D 12", "L 11"...
## $ R4 <chr> "W 14", "W 17", "W 21", "W 26", "D 13", "W 35"...
## $ R5 <chr> "W 7", "W 16", "W 11", "D 5", "D 4", "D 10"...
## $ R6 <chr> "D 12", "W 20", "W 13", "W 19", "W 14", "W 27"...
## $ R7 <chr> "D 4", "W 7", "W 12", "D 1", "W 17", "W 21"...
## $ State <chr> " ON ", " MI ", " MI ", " MI ", " MI ", " ...
## $ Rating <chr> " 15445895 / R: 1794 ->1817 ", " 14598900 / R:...
More discrete changes need to be made to specific columns to further clean the data and prepare for loading into a CSV
#Change the Rating column to remove everything except the rating
#NOTE: this probably should have been done in 1 step instead of 2 but for the life of me i can't figure out how to get these 2 RegEx expressions to play nice on the same line so to save my own sanity, I've split them into 2.
dt$Rating = dt$Rating %>% str_extract("(?<=R: )(.*).+?(?=->)") %>% str_extract("[^P]*")
#clean the round columns to contain only the IDs of the opponents, removing the win/loss stat
#NOTE: though loops are normally frowned upon in vectorized languages such as R, in this case it is acceptable as the number of loops is both known, and very short. this will not cause a noticable decrease in performance, nor will it show a measurable increase in runtime.
i = 4
while(i < 11) {
dt[, i] = str_replace(dt[, i], "[^0-9]", "")
i = i + 1
}
#Trim white space out of every column
for(c in 1:length(dt)) {
dt[, c] = trimws(dt[, c])
}
#Swap out IDs for ratings of opponents, convert to numeric as well.
i = 4
while(i < 11) {
dt[, i] = dt$Rating[match(unlist(dt[, i]), dt$ID)]
dt[, i] = as.numeric(as.character(dt[, i]))
i = i + 1
}
now that the data that we need is cleaned and formatted, I can assemble it into a data frame that matches the assigned format.
data_output = data.frame(
'Name' = dt$Name,
'State'= dt$State,
'Point_Total' = dt$`Total Points`,
'Player_Rating'= dt$Rating,
'Avg_Opp_Rating' = round(rowMeans(dt[,4:10], na.rm=TRUE), digits=0)
)
data_output
## Name State Point_Total Player_Rating Avg_Opp_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 LEMAN 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 STANESCU-BELLU 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 CORMICK 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
#Final output (row.names = FALSE needed to keep an ID column from appearing for some reason)
write.csv(data_output, "Chess_Scores_CLEAN.csv", row.names = FALSE)
Remaining questions and conclusions
#From the cross-tables, choose only the player’s opponents and average pre-rating of their opponents, both for players who played all of the scheduled games
played_all = data.frame(
'Name' = dt$Name,
'Player_Rating'= dt$Rating,
'Avg_Opp_Rating' = round(rowMeans(dt[,4:10], na.rm=FALSE), digits=0)
)
played_all = played_all %>% filter(!is.na(Avg_Opp_Rating))
played_all
## Name Player_Rating Avg_Opp_Rating
## 1 GARY HUA 1794 1605
## 2 DAKSHESH DARURI 1553 1469
## 3 ADITYA BAJAJ 1384 1564
## 4 PATRICK H SCHILLING 1716 1574
## 5 HANSHI ZUO 1655 1501
## 6 HANSEN SONG 1686 1519
## 7 GARY DEE SWATHELL 1649 1372
## 8 EZEKIEL HOUGHTON 1641 1468
## 9 STEFANO LEE 1411 1523
## 10 ANVIT RAO 1365 1554
## 11 CAMERON WILLIAM MC LEMAN 1712 1468
## 12 TORRANCE HENRY JR 1666 1498
## 13 BRADLEY SHAW 1610 1515
## 14 ZACHARY JAMES HOUGHTON 1220 1484
## 15 RONALD GRZEGORCZYK 1629 1499
## 16 DAVID SUNDEEN 1600 1480
## 17 DIPANKAR ROY 1564 1426
## 18 JASON ZHENG 1595 1411
## 19 DINH DANG BUI 1563 1470
## 20 ALAN BUI 1363 1214
## 21 MICHAEL R ALDRICH 1229 1357
## 22 LOREN SCHWIEBERT 1745 1363
## 23 MAX ZHU 1579 1507
## 24 SOFIA ADINA STANESCU-BELLU 1507 1522
## 25 GEORGE AVERY JONES 1522 1144
## 26 RISHI SHETTY 1494 1260
## 27 JOSHUA PHILIP MATHEWS 1441 1379
## 28 JADE GE 1449 1277
## 29 MICHAEL JEFFERY THOMAS 1399 1375
## 30 JOSHUA DAVID LEE 1438 1150
## 31 JOEL R HENDON 1436 1430
## 32 FOREST ZHANG 1348 1391
## 33 JARED GE 1332 1150
## 34 ROBERT GLEN VASEY 1283 1107
## 35 DEREK YAN 1242 1152
## 36 JACOB ALEXANDER LAVALLEY 377 1358
## 37 ERIC WRIGHT 1362 1392
## 38 TEJAS AYYAGARI 1011 1356
## 39 ETHAN GUO 935 1495
## 40 JEZZEL FARKAS 955 1327
## 41 BEN LI 1163 1263
#and for players who had one or more unplayed games (e.g. byes, forfeits)
played_some = data.frame(
'Name' = dt$Name,
'Player_Rating'= dt$Rating,
'Avg_Opp_Rating' = round(rowMeans(dt[,4:10], na.rm=TRUE), digits=0)
)