In this project, I was given a text file with chess tournament results where the information has some structure. My 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
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(data.table)
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:lubridate':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
##
## The following objects are masked from 'package:dplyr':
##
## between, first, last
##
## The following object is masked from 'package:purrr':
##
## transpose
library(stringr)
library(dplyr)
chess <- read.delim('https://raw.githubusercontent.com/xhuliaturkaj/data607_project_1/main/chess_project_raw.txt', skip = 4, header = FALSE, stringsAsFactors=FALSE)
head(chess)
## V1
## 1 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4|
## 2 ON | 15445895 / R: 1794 ->1817 |N:2 |W |B |W |B |W |B |W |
## 3 -----------------------------------------------------------------------------------------
## 4 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7|
## 5 MI | 14598900 / R: 1553 ->1663 |N:2 |B |W |B |W |B |W |B |
## 6 -----------------------------------------------------------------------------------------
chess is now a dataframe with one column V1 and multiple rows
#Let's start with removing all the rows that contain hyphens
chess <- subset(chess, !grepl("^-+$", V1))
# Next I will combine every two consecutive rows in one single row as each couple of rows contains info per one individual player
str(chess)
## 'data.frame': 128 obs. of 1 variable:
## $ V1: chr " 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4|" " ON | 15445895 / R: 1794 ->1817 |N:2 |W |B |W |B |W |B |W |" " 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7|" " MI | 14598900 / R: 1553 ->1663 |N:2 |B |W |B |W |B |W |B |" ...
chess_combined <- data.frame(V1 = paste(chess$V1[seq(1, nrow(chess), by = 2)],
chess$V1[seq(2, nrow(chess), by = 2)]))
chess_combined
## V1
## 1 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4| ON | 15445895 / R: 1794 ->1817 |N:2 |W |B |W |B |W |B |W |
## 2 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7| MI | 14598900 / R: 1553 ->1663 |N:2 |B |W |B |W |B |W |B |
## 3 3 | ADITYA BAJAJ |6.0 |L 8|W 61|W 25|W 21|W 11|W 13|W 12| MI | 14959604 / R: 1384 ->1640 |N:2 |W |B |W |B |W |B |W |
## 4 4 | PATRICK H SCHILLING |5.5 |W 23|D 28|W 2|W 26|D 5|W 19|D 1| MI | 12616049 / R: 1716 ->1744 |N:2 |W |B |W |B |W |B |B |
## 5 5 | HANSHI ZUO |5.5 |W 45|W 37|D 12|D 13|D 4|W 14|W 17| MI | 14601533 / R: 1655 ->1690 |N:2 |B |W |B |W |B |W |B |
## 6 6 | HANSEN SONG |5.0 |W 34|D 29|L 11|W 35|D 10|W 27|W 21| OH | 15055204 / R: 1686 ->1687 |N:3 |W |B |W |B |B |W |B |
## 7 7 | GARY DEE SWATHELL |5.0 |W 57|W 46|W 13|W 11|L 1|W 9|L 2| MI | 11146376 / R: 1649 ->1673 |N:3 |W |B |W |B |B |W |W |
## 8 8 | EZEKIEL HOUGHTON |5.0 |W 3|W 32|L 14|L 9|W 47|W 28|W 19| MI | 15142253 / R: 1641P17->1657P24 |N:3 |B |W |B |W |B |W |W |
## 9 9 | STEFANO LEE |5.0 |W 25|L 18|W 59|W 8|W 26|L 7|W 20| ON | 14954524 / R: 1411 ->1564 |N:2 |W |B |W |B |W |B |B |
## 10 10 | ANVIT RAO |5.0 |D 16|L 19|W 55|W 31|D 6|W 25|W 18| MI | 14150362 / R: 1365 ->1544 |N:3 |W |W |B |B |W |B |W |
## 11 11 | CAMERON WILLIAM MC LEMAN |4.5 |D 38|W 56|W 6|L 7|L 3|W 34|W 26| MI | 12581589 / R: 1712 ->1696 |N:3 |B |W |B |W |B |W |B |
## 12 12 | KENNETH J TACK |4.5 |W 42|W 33|D 5|W 38|H |D 1|L 3| MI | 12681257 / R: 1663 ->1670 |N:3 |W |B |W |B | |W |B |
## 13 13 | TORRANCE HENRY JR |4.5 |W 36|W 27|L 7|D 5|W 33|L 3|W 32| MI | 15082995 / R: 1666 ->1662 |N:3 |B |W |B |B |W |W |B |
## 14 14 | BRADLEY SHAW |4.5 |W 54|W 44|W 8|L 1|D 27|L 5|W 31| MI | 10131499 / R: 1610 ->1618 |N:3 |W |B |W |W |B |B |W |
## 15 15 | ZACHARY JAMES HOUGHTON |4.5 |D 19|L 16|W 30|L 22|W 54|W 33|W 38| MI | 15619130 / R: 1220P13->1416P20 |N:3 |B |B |W |W |B |B |W |
## 16 16 | MIKE NIKITIN |4.0 |D 10|W 15|H |W 39|L 2|W 36|U | MI | 10295068 / R: 1604 ->1613 |N:3 |B |W | |B |W |B | |
## 17 17 | RONALD GRZEGORCZYK |4.0 |W 48|W 41|L 26|L 2|W 23|W 22|L 5| MI | 10297702 / R: 1629 ->1610 |N:3 |W |B |W |B |W |B |W |
## 18 18 | DAVID SUNDEEN |4.0 |W 47|W 9|L 1|W 32|L 19|W 38|L 10| MI | 11342094 / R: 1600 ->1600 |N:3 |B |W |B |W |B |W |B |
## 19 19 | DIPANKAR ROY |4.0 |D 15|W 10|W 52|D 28|W 18|L 4|L 8| MI | 14862333 / R: 1564 ->1570 |N:3 |W |B |W |B |W |W |B |
## 20 20 | JASON ZHENG |4.0 |L 40|W 49|W 23|W 41|W 28|L 2|L 9| MI | 14529060 / R: 1595 ->1569 |N:4 |W |B |W |B |W |B |W |
## 21 21 | DINH DANG BUI |4.0 |W 43|L 1|W 47|L 3|W 40|W 39|L 6| ON | 15495066 / R: 1563P22->1562 |N:3 |B |W |B |W |W |B |W |
## 22 22 | EUGENE L MCCLURE |4.0 |W 64|D 52|L 28|W 15|H |L 17|W 40| MI | 12405534 / R: 1555 ->1529 |N:4 |W |B |W |B | |W |B |
## 23 23 | ALAN BUI |4.0 |L 4|W 43|L 20|W 58|L 17|W 37|W 46| ON | 15030142 / R: 1363 ->1371 | |B |W |B |W |B |W |B |
## 24 24 | MICHAEL R ALDRICH |4.0 |L 28|L 47|W 43|L 25|W 60|W 44|W 39| MI | 13469010 / R: 1229 ->1300 |N:4 |B |W |B |B |W |W |B |
## 25 25 | LOREN SCHWIEBERT |3.5 |L 9|W 53|L 3|W 24|D 34|L 10|W 47| MI | 12486656 / R: 1745 ->1681 |N:4 |B |W |B |W |B |W |B |
## 26 26 | MAX ZHU |3.5 |W 49|W 40|W 17|L 4|L 9|D 32|L 11| ON | 15131520 / R: 1579 ->1564 |N:4 |B |W |B |W |B |W |W |
## 27 27 | GAURAV GIDWANI |3.5 |W 51|L 13|W 46|W 37|D 14|L 6|U | MI | 14476567 / R: 1552 ->1539 |N:4 |W |B |W |B |W |B | |
## 28 28 | SOFIA ADINA STANESCU-BELLU |3.5 |W 24|D 4|W 22|D 19|L 20|L 8|D 36| MI | 14882954 / R: 1507 ->1513 |N:3 |W |W |B |W |B |B |W |
## 29 29 | CHIEDOZIE OKORIE |3.5 |W 50|D 6|L 38|L 34|W 52|W 48|U | MI | 15323285 / R: 1602P6 ->1508P12 |N:4 |B |W |B |W |W |B | |
## 30 30 | GEORGE AVERY JONES |3.5 |L 52|D 64|L 15|W 55|L 31|W 61|W 50| ON | 12577178 / R: 1522 ->1444 | |W |B |B |W |W |B |B |
## 31 31 | RISHI SHETTY |3.5 |L 58|D 55|W 64|L 10|W 30|W 50|L 14| MI | 15131618 / R: 1494 ->1444 | |B |W |B |W |B |W |B |
## 32 32 | JOSHUA PHILIP MATHEWS |3.5 |W 61|L 8|W 44|L 18|W 51|D 26|L 13| ON | 14073750 / R: 1441 ->1433 |N:4 |W |B |W |B |W |B |W |
## 33 33 | JADE GE |3.5 |W 60|L 12|W 50|D 36|L 13|L 15|W 51| MI | 14691842 / R: 1449 ->1421 | |B |W |B |W |B |W |B |
## 34 34 | MICHAEL JEFFERY THOMAS |3.5 |L 6|W 60|L 37|W 29|D 25|L 11|W 52| MI | 15051807 / R: 1399 ->1400 | |B |W |B |B |W |B |W |
## 35 35 | JOSHUA DAVID LEE |3.5 |L 46|L 38|W 56|L 6|W 57|D 52|W 48| MI | 14601397 / R: 1438 ->1392 | |W |W |B |W |B |B |W |
## 36 36 | SIDDHARTH JHA |3.5 |L 13|W 57|W 51|D 33|H |L 16|D 28| MI | 14773163 / R: 1355 ->1367 |N:4 |W |B |W |B | |W |B |
## 37 37 | AMIYATOSH PWNANANDAM |3.5 |B |L 5|W 34|L 27|H |L 23|W 61| MI | 15489571 / R: 980P12->1077P17 | | |B |W |W | |B |W |
## 38 38 | BRIAN LIU |3.0 |D 11|W 35|W 29|L 12|H |L 18|L 15| MI | 15108523 / R: 1423 ->1439 |N:4 |W |B |W |W | |B |B |
## 39 39 | JOEL R HENDON |3.0 |L 1|W 54|W 40|L 16|W 44|L 21|L 24| MI | 12923035 / R: 1436P23->1413 |N:4 |B |W |B |W |B |W |W |
## 40 40 | FOREST ZHANG |3.0 |W 20|L 26|L 39|W 59|L 21|W 56|L 22| MI | 14892710 / R: 1348 ->1346 | |B |B |W |W |B |W |W |
## 41 41 | KYLE WILLIAM MURPHY |3.0 |W 59|L 17|W 58|L 20|X |U |U | MI | 15761443 / R: 1403P5 ->1341P9 | |B |W |B |W | | | |
## 42 42 | JARED GE |3.0 |L 12|L 50|L 57|D 60|D 61|W 64|W 56| MI | 14462326 / R: 1332 ->1256 | |B |W |B |B |W |W |B |
## 43 43 | ROBERT GLEN VASEY |3.0 |L 21|L 23|L 24|W 63|W 59|L 46|W 55| MI | 14101068 / R: 1283 ->1244 | |W |B |W |W |B |B |W |
## 44 44 | JUSTIN D SCHILLING |3.0 |B |L 14|L 32|W 53|L 39|L 24|W 59| MI | 15323504 / R: 1199 ->1199 | | |W |B |B |W |B |W |
## 45 45 | DEREK YAN |3.0 |L 5|L 51|D 60|L 56|W 63|D 55|W 58| MI | 15372807 / R: 1242 ->1191 | |W |B |W |B |W |B |W |
## 46 46 | JACOB ALEXANDER LAVALLEY |3.0 |W 35|L 7|L 27|L 50|W 64|W 43|L 23| MI | 15490981 / R: 377P3 ->1076P10 | |B |W |B |W |B |W |W |
## 47 47 | ERIC WRIGHT |2.5 |L 18|W 24|L 21|W 61|L 8|D 51|L 25| MI | 12533115 / R: 1362 ->1341 | |W |B |W |B |W |B |W |
## 48 48 | DANIEL KHAIN |2.5 |L 17|W 63|H |D 52|H |L 29|L 35| MI | 14369165 / R: 1382 ->1335 | |B |W | |B | |W |B |
## 49 49 | MICHAEL J MARTIN |2.5 |L 26|L 20|D 63|D 64|W 58|H |U | MI | 12531685 / R: 1291P12->1259P17 | |W |W |B |W |B | | |
## 50 50 | SHIVAM JHA |2.5 |L 29|W 42|L 33|W 46|H |L 31|L 30| MI | 14773178 / R: 1056 ->1111 | |W |B |W |B | |B |W |
## 51 51 | TEJAS AYYAGARI |2.5 |L 27|W 45|L 36|W 57|L 32|D 47|L 33| MI | 15205474 / R: 1011 ->1097 | |B |W |B |W |B |W |W |
## 52 52 | ETHAN GUO |2.5 |W 30|D 22|L 19|D 48|L 29|D 35|L 34| MI | 14918803 / R: 935 ->1092 |N:4 |B |W |B |W |B |W |B |
## 53 53 | JOSE C YBARRA |2.0 |H |L 25|H |L 44|U |W 57|U | MI | 12578849 / R: 1393 ->1359 | | |B | |W | |W | |
## 54 54 | LARRY HODGE |2.0 |L 14|L 39|L 61|B |L 15|L 59|W 64| MI | 12836773 / R: 1270 ->1200 | |B |B |W | |W |B |W |
## 55 55 | ALEX KONG |2.0 |L 62|D 31|L 10|L 30|B |D 45|L 43| MI | 15412571 / R: 1186 ->1163 | |W |B |W |B | |W |B |
## 56 56 | MARISA RICCI |2.0 |H |L 11|L 35|W 45|H |L 40|L 42| MI | 14679887 / R: 1153 ->1140 | | |B |W |W | |B |W |
## 57 57 | MICHAEL LU |2.0 |L 7|L 36|W 42|L 51|L 35|L 53|B | MI | 15113330 / R: 1092 ->1079 | |B |W |W |B |W |B | |
## 58 58 | VIRAJ MOHILE |2.0 |W 31|L 2|L 41|L 23|L 49|B |L 45| MI | 14700365 / R: 917 -> 941 | |W |B |W |B |W | |B |
## 59 59 | SEAN M MC CORMICK |2.0 |L 41|B |L 9|L 40|L 43|W 54|L 44| MI | 12841036 / R: 853 -> 878 | |W | |B |B |W |W |B |
## 60 60 | JULIA SHEN |1.5 |L 33|L 34|D 45|D 42|L 24|H |U | MI | 14579262 / R: 967 -> 984 | |W |B |B |W |B | | |
## 61 61 | JEZZEL FARKAS |1.5 |L 32|L 3|W 54|L 47|D 42|L 30|L 37| ON | 15771592 / R: 955P11-> 979P18 | |B |W |B |W |B |W |B |
## 62 62 | ASHWIN BALAJI |1.0 |W 55|U |U |U |U |U |U | MI | 15219542 / R: 1530 ->1535 | |B | | | | | | |
## 63 63 | THOMAS JOSEPH HOSMER |1.0 |L 2|L 48|D 49|L 43|L 45|H |U | MI | 15057092 / R: 1175 ->1125 | |W |B |W |B |B | | |
## 64 64 | BEN LI |1.0 |L 22|D 30|L 31|D 49|L 46|L 42|L 54| MI | 15006561 / R: 1163 ->1112 | |B |W |W |B |W |B |B |
For each observation the information after the arrow -> is unnecessary for my ultimate csv file so I will substitute it with empty string
chess_combined$V1 <- sub("->.*", "", chess_combined$V1)
head(chess_combined$V1)
## [1] " 1 | GARY HUA |6.0 |W 39|W 21|W 18|W 14|W 7|D 12|D 4| ON | 15445895 / R: 1794 "
## [2] " 2 | DAKSHESH DARURI |6.0 |W 63|W 58|L 4|W 17|W 16|W 20|W 7| MI | 14598900 / R: 1553 "
## [3] " 3 | ADITYA BAJAJ |6.0 |L 8|W 61|W 25|W 21|W 11|W 13|W 12| MI | 14959604 / R: 1384 "
## [4] " 4 | PATRICK H SCHILLING |5.5 |W 23|D 28|W 2|W 26|D 5|W 19|D 1| MI | 12616049 / R: 1716 "
## [5] " 5 | HANSHI ZUO |5.5 |W 45|W 37|D 12|D 13|D 4|W 14|W 17| MI | 14601533 / R: 1655 "
## [6] " 6 | HANSEN SONG |5.0 |W 34|D 29|L 11|W 35|D 10|W 27|W 21| OH | 15055204 / R: 1686 "
Next I have created the Rounds Columns
column_names <- c("Player_Number", "Player_Name", "Total_Pts", "Round_1", "Round_2", "Round_3",
"Round_4", "Round_5", "Round_6", "Round_7", "State", "Rating")
chess_combined <- chess_combined %>%
separate(V1, into = column_names, sep = "\\|")
For the Columns “Round_1”, “Round_2”, “Round_3”, “Round_4”, “Round_5”, “Round_6” and “Round_7” I only need the numerical values for each observations
rounds_columns <- c("Round_1", "Round_2", "Round_3", "Round_4", "Round_5", "Round_6", "Round_7")
#This function will extracte just the numerical values
extract_numeric <- function(x) {
as.numeric(str_extract(x, "\\d+"))
}
chess_combined <- chess_combined %>%
mutate(across(all_of(rounds_columns), extract_numeric))
For the Rating column the true rating value is the numerical value after R: so I will extract that value only
chess_combined <- chess_combined %>%
mutate(Rating = str_extract(str_replace(Rating, "^.*\\/ R: ", ""), "\\d+"))
Next I want to change the values of all 7 round columns from the player number to the players rating, this will make the Opponent Avg Rating easier to calculate
# make sure the rounds columns have numeric values
chess_combined <- chess_combined %>%
mutate(across(all_of(rounds_columns), as.numeric))
chess_lookup_table <- data.frame(
Opponent_Rating = as.numeric(as.character(chess_combined$Rating)),
Player_Number = as.numeric(as.character(chess_combined$Player_Number))
)
chess_lookup_table
## Opponent_Rating Player_Number
## 1 1794 1
## 2 1553 2
## 3 1384 3
## 4 1716 4
## 5 1655 5
## 6 1686 6
## 7 1649 7
## 8 1641 8
## 9 1411 9
## 10 1365 10
## 11 1712 11
## 12 1663 12
## 13 1666 13
## 14 1610 14
## 15 1220 15
## 16 1604 16
## 17 1629 17
## 18 1600 18
## 19 1564 19
## 20 1595 20
## 21 1563 21
## 22 1555 22
## 23 1363 23
## 24 1229 24
## 25 1745 25
## 26 1579 26
## 27 1552 27
## 28 1507 28
## 29 1602 29
## 30 1522 30
## 31 1494 31
## 32 1441 32
## 33 1449 33
## 34 1399 34
## 35 1438 35
## 36 1355 36
## 37 980 37
## 38 1423 38
## 39 1436 39
## 40 1348 40
## 41 1403 41
## 42 1332 42
## 43 1283 43
## 44 1199 44
## 45 1242 45
## 46 377 46
## 47 1362 47
## 48 1382 48
## 49 1291 49
## 50 1056 50
## 51 1011 51
## 52 935 52
## 53 1393 53
## 54 1270 54
## 55 1186 55
## 56 1153 56
## 57 1092 57
## 58 917 58
## 59 853 59
## 60 967 60
## 61 955 61
## 62 1530 62
## 63 1175 63
## 64 1163 64
# Replace the player numbers in round columns with their corresponding ratings
chess_combined <- chess_combined %>%
rowwise() %>%
mutate(across(all_of(rounds_columns),
~ ifelse(is.na(.), NA,
chess_lookup_table$Opponent_Rating[match(., chess_lookup_table$Player_Number)])))
Next I will create an extra column that represent the average value of the total Opponents Rating
chess_combined <- chess_combined %>%
rowwise() %>%
mutate(Average_Opponent_Rating = mean(c_across(all_of(rounds_columns)), na.rm = TRUE)) %>%
ungroup()
The final step is to subset the df with only our columns of interest and to create a csv file
final_chess_df <- chess_combined %>% select(Player_Name, State,Total_Pts, Rating, Average_Opponent_Rating)%>%
mutate( Average_Opponent_Rating= round(Average_Opponent_Rating))
write.csv(final_chess_df, "final_chess_df.csv", row.names = FALSE)