For this project I used the following code to initialize my data. I included comments to explain what I was doing, and why.
library(RCurl)
## Loading required package: bitops
library(stringr)
#Extracting the URL text
chessURL <-getURL("https://raw.githubusercontent.com/mfarris9505/Project1Data/master/Chess%20Players.txt")
#Reading the txt file into "CSV"
chess <-read.csv(text = chessURL)
#There was this odd first line of X..... which I eliminated
names(chess)[1] <- "header"
From my experience with Python and game creation, I was more comfortable looking at this data set and dealing with position. I am new to the regex, and I understand why it would and could be useful, but in this instance, I decided to first test position, and to see if I could develop a standard set and then replicate the position for the various lines in the rough data. So, we took some data points to start.
str_extract(chess$header, "GARY")
str_locate(chess$header, "GARY")
*Note I didn’t run this code in the Markdown, as it produces just multiple lines and isn’t all that pretty. I ran it in the console, and viewed the output, and took note of the positions.
Much to my delight… I found that the 2 Gary’s in the data set were in the same position 9 to 12.
I tested this with the score as well, to see if they were in similar positions, and I found out that I was indeed correct, everything corrolated to a relative postion (Again, I tested the points by plugging in test values such as “39”, “21”, and “18”, and ran it in the console. This process was not shown here) and found that the each are 6 positions away starting at position 51.
Repeating this for each data point I needed (Name, State, Score, and AvG) I found the positions which can be seen in the code below. This was actually quite simple and took me roughly 5 minutes. I figured it save me about an hour trying to code regex to pick out specific delimiters.
Using a for loop, I ran over the data, and extracted everything I needed.
N64 <- 1:64
chessdata <- data.frame(N64)
chessopponents <- data.frame(N64)
x <-4
for (i in 1:64) {
chessdata$Name[i] <- str_sub(chess$header[x], start = 9, end = 35)
chessdata$State[i] <- str_sub(chess$header[x+1], start = 4, end = 5)
chessdata$Score[i] <- str_sub(chess$header[x], start = 42, end = 44)
chessdata$AVG[i] <- str_sub(chess$header[x+1], start = 23, end = 26)
chessopponents$RD1[i] <-str_sub(chess$header[x], start = 51, end = 52)
chessopponents$RD2[i] <-str_sub(chess$header[x], start = 57, end = 58)
chessopponents$RD3[i] <-str_sub(chess$header[x], start = 63, end = 64)
chessopponents$RD4[i] <-str_sub(chess$header[x], start = 69, end = 70)
chessopponents$RD5[i] <-str_sub(chess$header[x], start = 75, end = 76)
chessopponents$RD6[i] <-str_sub(chess$header[x], start = 81, end = 82)
chessopponents$RD7[i] <-str_sub(chess$header[x], start = 87, end = 88)
x <-x+3
}
# Formating to force String to numeric for the calculations
chessdata$AVG <- as.numeric(as.character(chessdata$AVG))
chessopponents$RD1 <- as.numeric(as.character(chessopponents$RD1))
chessopponents$RD2 <- as.numeric(as.character(chessopponents$RD2))
chessopponents$RD3 <- as.numeric(as.character(chessopponents$RD3))
chessopponents$RD4 <- as.numeric(as.character(chessopponents$RD4))
chessopponents$RD5 <- as.numeric(as.character(chessopponents$RD5))
chessopponents$RD6 <- as.numeric(as.character(chessopponents$RD6))
chessopponents$RD7 <- as.numeric(as.character(chessopponents$RD7))
head(chessdata)
## N64 Name State Score AVG
## 1 1 GARY HUA ON 6.0 1794
## 2 2 DAKSHESH DARURI MI 6.0 1553
## 3 3 ADITYA BAJAJ MI 6.0 1384
## 4 4 PATRICK H SCHILLING MI 5.5 1716
## 5 5 HANSHI ZUO MI 5.5 1655
## 6 6 HANSEN SONG OH 5.0 1686
head(chessopponents)
## N64 RD1 RD2 RD3 RD4 RD5 RD6 RD7
## 1 1 39 21 18 14 7 12 4
## 2 2 63 58 4 17 16 20 7
## 3 3 8 61 25 21 11 13 12
## 4 4 23 28 2 26 5 19 1
## 5 5 45 37 12 13 4 14 17
## 6 6 34 29 11 35 10 27 21
From here, calculating the averages could be accomplished in several ways, however, again from my experience with Python, I went ahead and composed a double iterated “for” loop, with an “if” statement to weed out any of the coerced NA values. Again, probably not the best way to accomplish this task, but as the data set was not signiciantly large, I felt it would accomplish the task quite well.
x <- 0
avgadd <- 0
for (i in 1:64){
for (j in 1:7){
#Test to rule out NA values
if (is.na(chessopponents[i,j+1])){
#Else statement only adds the number of player
}else{
avgadd<- avgadd + chessdata$AVG[chessopponents[i,j+1]]
x<- x + 1
}
}
chessdata$Avg_OP[i] <- avgadd/x
avgadd <-0
x<-0
}
head(chessdata)
## N64 Name State Score AVG Avg_OP
## 1 1 GARY HUA ON 6.0 1794 1605.286
## 2 2 DAKSHESH DARURI MI 6.0 1553 1469.286
## 3 3 ADITYA BAJAJ MI 6.0 1384 1563.571
## 4 4 PATRICK H SCHILLING MI 5.5 1716 1573.571
## 5 5 HANSHI ZUO MI 5.5 1655 1500.857
## 6 6 HANSEN SONG OH 5.0 1686 1518.714
I included the entire list at the bottom, for readability. To write the table to a CSV, the following code was written. A couple notes, I left the data as it was in the beginning as I knew it wasn’t particularly large. However, now that we are saving the data for future use, some things that would be benficial would be to eliminate “excessive data.” In the beginning, I captured elongated names (ie. the spaces behind the names, as I was unsure how long each name was). Had this been an extremely large dataset this could impact processing so, the str_trim() was used to trim the fat as it were, and then the trimmed file could be saved. I uploaded the file to my github repository and pasted the link with the hwk assignment:
chessdata$Name <- str_trim(chessdata$Name)
head(chessdata)
## N64 Name State Score AVG Avg_OP
## 1 1 GARY HUA ON 6.0 1794 1605.286
## 2 2 DAKSHESH DARURI MI 6.0 1553 1469.286
## 3 3 ADITYA BAJAJ MI 6.0 1384 1563.571
## 4 4 PATRICK H SCHILLING MI 5.5 1716 1573.571
## 5 5 HANSHI ZUO MI 5.5 1655 1500.857
## 6 6 HANSEN SONG OH 5.0 1686 1518.714
write.table(chessdata, "MyChessdata.csv")
#All of the data for review
chessdata
## N64 Name State Score AVG Avg_OP
## 1 1 GARY HUA ON 6.0 1794 1605.286
## 2 2 DAKSHESH DARURI MI 6.0 1553 1469.286
## 3 3 ADITYA BAJAJ MI 6.0 1384 1563.571
## 4 4 PATRICK H SCHILLING MI 5.5 1716 1573.571
## 5 5 HANSHI ZUO MI 5.5 1655 1500.857
## 6 6 HANSEN SONG OH 5.0 1686 1518.714
## 7 7 GARY DEE SWATHELL MI 5.0 1649 1372.143
## 8 8 EZEKIEL HOUGHTON MI 5.0 1641 1468.429
## 9 9 STEFANO LEE ON 5.0 1411 1523.143
## 10 10 ANVIT RAO MI 5.0 1365 1554.143
## 11 11 CAMERON WILLIAM MC LEMAN MI 4.5 1712 1467.571
## 12 12 KENNETH J TACK MI 4.5 1663 1506.167
## 13 13 TORRANCE HENRY JR MI 4.5 1666 1497.857
## 14 14 BRADLEY SHAW MI 4.5 1610 1515.000
## 15 15 ZACHARY JAMES HOUGHTON MI 4.5 1220 1483.857
## 16 16 MIKE NIKITIN MI 4.0 1604 1385.800
## 17 17 RONALD GRZEGORCZYK MI 4.0 1629 1498.571
## 18 18 DAVID SUNDEEN MI 4.0 1600 1480.000
## 19 19 DIPANKAR ROY MI 4.0 1564 1426.286
## 20 20 JASON ZHENG MI 4.0 1595 1410.857
## 21 21 DINH DANG BUI ON 4.0 1563 1470.429
## 22 22 EUGENE L MCCLURE MI 4.0 1555 1300.333
## 23 23 ALAN BUI ON 4.0 1363 1213.857
## 24 24 MICHAEL R ALDRICH MI 4.0 1229 1357.000
## 25 25 LOREN SCHWIEBERT MI 3.5 1745 1363.286
## 26 26 MAX ZHU ON 3.5 1579 1506.857
## 27 27 GAURAV GIDWANI MI 3.5 1552 1221.667
## 28 28 SOFIA ADINA STANESCU-BELLU MI 3.5 1507 1522.143
## 29 29 CHIEDOZIE OKORIE MI 3.5 1602 1313.500
## 30 30 GEORGE AVERY JONES ON 3.5 1522 1144.143
## 31 31 RISHI SHETTY MI 3.5 1494 1259.857
## 32 32 JOSHUA PHILIP MATHEWS ON 3.5 1441 1378.714
## 33 33 JADE GE MI 3.5 1449 1276.857
## 34 34 MICHAEL JEFFERY THOMAS MI 3.5 1399 1375.286
## 35 35 JOSHUA DAVID LEE MI 3.5 1438 1149.714
## 36 36 SIDDHARTH JHA MI 3.5 1355 1388.167
## 37 37 AMIYATOSH PWNANANDAM MI 3.5 980 1384.800
## 38 38 BRIAN LIU MI 3.0 1423 1539.167
## 39 39 JOEL R HENDON MI 3.0 1436 1429.571
## 40 40 FOREST ZHANG MI 3.0 1348 1390.571
## 41 41 KYLE WILLIAM MURPHY MI 3.0 1403 1248.500
## 42 42 JARED GE MI 3.0 1332 1149.857
## 43 43 ROBERT GLEN VASEY MI 3.0 1283 1106.571
## 44 44 JUSTIN D SCHILLING MI 3.0 1199 1327.000
## 45 45 DEREK YAN MI 3.0 1242 1152.000
## 46 46 JACOB ALEXANDER LAVALLEY MI 3.0 377 1357.714
## 47 47 ERIC WRIGHT MI 2.5 1362 1392.000
## 48 48 DANIEL KHAIN MI 2.5 1382 1355.800
## 49 49 MICHAEL J MARTIN MI 2.5 1291 1285.800
## 50 50 SHIVAM JHA MI 2.5 1056 1296.000
## 51 51 TEJAS AYYAGARI MI 2.5 1011 1356.143
## 52 52 ETHAN GUO MI 2.5 935 1494.571
## 53 53 JOSE C YBARRA MI 2.0 1393 1345.333
## 54 54 LARRY HODGE MI 2.0 1270 1206.167
## 55 55 ALEX KONG MI 2.0 1186 1406.000
## 56 56 MARISA RICCI MI 2.0 1153 1414.400
## 57 57 MICHAEL LU MI 2.0 1092 1363.000
## 58 58 VIRAJ MOHILE MI 2.0 917 1391.000
## 59 59 SEAN M MC CORMICK MI 2.0 853 1319.000
## 60 60 JULIA SHEN MI 1.5 967 1330.200
## 61 61 JEZZEL FARKAS ON 1.5 955 1327.286
## 62 62 ASHWIN BALAJI MI 1.0 1530 1186.000
## 63 63 THOMAS JOSEPH HOSMER MI 1.0 1175 1350.200
## 64 64 BEN LI MI 1.0 1163 1263.000