forked from 321k/Google-Trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2. Scoring GT
58 lines (46 loc) · 1.42 KB
/
2. Scoring GT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
scoringGT=function(players, competitionsGToutput){
#"players" must be a data frame with at least one column
scoreBoard=data.frame()
if(nrow(competitionsGToutput[-which(competitionsGToutput[,1]=="Winner"),])>0){
competitionsGToutput=competitionsGToutput[-which(competitionsGToutput[,1]=="Winner"),]
}
competitionsGToutput[,1]=as.numeric(competitionsGToutput[,1])
for(i in 1:max(competitionsGToutput[1:(nrow(competitionsGToutput)),1])) {
batch=competitionsGToutput[which(competitionsGToutput[,1]==i),]
highScore=max(batch[,3])
roundWinner=batch[which(batch[,3]==highScore),]
batch[,4]=batch[,3]/roundWinner[,3]
colnames(batch)[4]="Multiplier"
scoreBoard=rbind(scoreBoard, batch)
colnames(scoreBoard)=c("Round", "Player", "Score", "Multiplier")
}
M=as.data.frame(matrix(NA, nrow(players), nrow(players)))
iterationMax=sum(is.na(M))
colnames(M)=players[,1]
rownames(M)=players[,1]
for(j in 1:max(scoreBoard[,1])){
batch=scoreBoard[which(scoreBoard[,1]==j),]
for(i in 1:nrow(batch)){
place=which(tolower(rownames(M))==batch[i,2])
roundWinner=batch[which(batch[,4]==1),2]
M[place, which(tolower(colnames(M))==roundWinner)]=batch[i,4]
}
}
for(y in 1:nrow(players)){
for(x in 1:nrow(players)){
if(is.na(M[y,x])) M[y,x]=1/M[x,y]
M[y,y]=1
}
}
while((iterationMax-sum(is.na(M)))>0){
iterationMax=sum(is.na(M))
for(i in 1:nrow(players)){
for(x in 1:nrow(players)){
for(y in 1:nrow(players)){
if(is.na(M[y,i])) M[y,i]=M[y,x]/M[i,x]
}
}
}
}
return(M)
}