packages:
#install.packages("tidyverse", repos = "http://cran.us.r-project.org")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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(dplyr)
library(tidyr)
read table from github:
#load it in
my_data <- read.table("https://raw.githubusercontent.com/GuillermoCharlesSchneider/DATA-607/main/Project-1/tournamentinfo.txt", sep ="|", fill = TRUE)
cleaning:
# data frame, and
my_data <- data.frame(my_data[-1, ])
#remove every third line to remove the dashes
my_data <- my_data[-(seq(3,to=nrow(my_data),by=3)),]
#remove the empty last column
my_data <- my_data[-11]
#remove header
my_data <- data.frame(my_data[-(1:2), ])
split into two, every other line, and remerge:
L <- my_data[-(seq(2,to=nrow(my_data),by=2)),]
R <- my_data[-(seq(1,to=nrow(my_data),by=2)),]
updated <- cbind(L, R)
updated <- updated[-(13:20)] #remove the W and B playing colors
clean pre and post, and remove the provisional scores, seperate on any nonalphanumeric:
seperated <- updated %>% separate(12, c(NA, 'ID', NA,'Pre', 'Post'), extra="merge")
seperated <- seperated %>% separate(13, sep = 'P', c('Pre', NA), extra="merge")
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 54 rows [1, 2, 3, 4, 5,
## 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 22, 23, ...].
seperated <- seperated %>% separate(14, sep = 'P', c('Post', NA), extra="merge")
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 56 rows [1, 2, 3, 4, 5,
## 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, ...].
remove W and L:
seperated$V4 <- str_remove_all(seperated$V4, "\\D")
seperated$V5 <- str_remove_all(seperated$V5, "\\D")
seperated$V6 <- str_remove_all(seperated$V6, "\\D")
seperated$V7<- str_remove_all(seperated$V7, "\\D")
seperated$V8 <- str_remove_all(seperated$V8, "\\D")
seperated$V9 <- str_remove_all(seperated$V9, "\\D")
seperated$V10 <- str_remove_all(seperated$V10, "\\D")
FIND opps score. if not NA, replace with the opps score, repeat for each rounds column:
avg_scores <- seperated
#columns, rounds 1-6
for(j in 4:10){
#rows, all 64 players
for (i in 1:64){
if (!is.na(avg_scores[i,j]))
{avg_scores[i,j] <- avg_scores[as.numeric(avg_scores[i,j]),13]
} else
avg_scores[i,j] <- NA
}
}
row means:
#char to numeric
avg_scores[, c(4:10)] <- sapply(avg_scores[, c(4:10)], as.numeric)
#row means, ignore na, and round
avg_scores$v15 <- rowMeans(avg_scores[,c(4:10)], na.rm=T)
avg_scores$v15 <- round(avg_scores$v15)
#grab just the columns we need
final <- avg_scores[,c(2,11,3,13,15)]
colnames(final) <- c("Player’s Name","Player’s State","Total Number of Points","Player’s Pre-Rating","Average Pre Chess Rating of Opponents")
print(final[c(1:5),])
## Player’s Name Player’s State Total Number of Points
## 5 GARY HUA ON 6.0
## 8 DAKSHESH DARURI MI 6.0
## 11 ADITYA BAJAJ MI 6.0
## 14 PATRICK H SCHILLING MI 5.5
## 17 HANSHI ZUO MI 5.5
## Player’s Pre-Rating Average Pre Chess Rating of Opponents
## 5 1794 1605
## 8 1553 1469
## 11 1384 1564
## 14 1716 1574
## 17 1655 1501
**CSV:*
#csv
write.csv(final, "chessELO.csv", row.names=FALSE)