-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordsFunction.R
More file actions
122 lines (113 loc) · 5.39 KB
/
RecordsFunction.R
File metadata and controls
122 lines (113 loc) · 5.39 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# dat <- read.csv('RenegadesHistoryFormatted.csv')
# weekly = F
# Season = c(2016, 2017)
# statcat = 'HR'
# playoffs = F
# best = T
# numshow = 10
# ownerfilter = 'All'
# franchisefilter = 'All'
records.func <- function(dat = dat,
weekly = T,
season = c(2011:2017),
statcat = 'All',
playoffs = F,
best = T,
numshow = 5,
ownerfilter = 'All',
franchisefilter = 'All'){
dat <- dat %>% filter(AllStar == 0) %>%
mutate(Luck = Wins - xWins)
dat <- dat %>% filter(ifelse(rep(ownerfilter, nrow(dat)) == 'All',
TRUE,
TeamOwner == ownerfilter))
dat <- dat %>% filter(ifelse(rep(franchisefilter, nrow(dat)) == 'All',
TRUE,
CurrentName == franchisefilter))
if(!weekly){
dat <- dat %>%
filter(Playoffs == as.numeric(playoffs)) %>%
group_by(Season, Team) %>%
summarize(Owner = TeamOwner[1],
R = sum(R),
HR = sum(HR),
RBI = sum(RBI),
SB = sum(SB),
OBP = mean(OBP),
SLG = mean(SLG),
K = sum(K),
QS = sum(QS),
W = sum(W),
SV = sum(SV),
ERA = mean(ERA),
WHIP = mean(WHIP),
Luck = sum(Luck)) %>%
ungroup()
return(dat %>%
select_(.dots = c('Team', 'Season', 'Owner', statcat)) %>%
arrange_(ifelse((statcat %in% c('WHIP', 'ERA') & best) | (!(statcat %in% c('WHIP', 'ERA')) & !best), statcat, paste0('desc(', statcat, ')'))) %>%
filter(Season %in% season) %>%
filter(row_number() <= numshow))
} else{
if(statcat == 'All'){
if(playoffs){
ranks <- data.frame(Team = dat$Team[dat$Playoffs == 1 & dat$Season %in% season],
Season = dat$Season[dat$Playoffs == 1 & dat$Season %in% season],
Week = dat$Week[dat$Playoffs == 1 & dat$Season %in% season],
sapply(dat %>% filter(Playoffs == 1 & Season %in% season) %>%
select(R:SV), rank),
sapply(dat %>% filter(dat$Playoffs == 1) %>%
select(ERA, WHIP), function(x) nrow(dat) + 1 - rank(x)))
ranks[['Score']] <- apply(ranks %>% select(R:WHIP), 1, sum)
return(ranks %>%
arrange_(ifelse(best, 'desc(Score)', 'Score')) %>%
filter(row_number() <= numshow) %>%
select(Team, Season, Week, Score) %>%
left_join(dat %>%
select(Team, Season, Week, Owner = TeamOwner, R:WHIP),
c('Team', 'Season', 'Week')) %>%
mutate(WinPct = Score / (nrow(dat[dat$Playoffs == as.numeric(playoffs) & dat$Season %in% season,]) * 12)) %>%
select(-Score))
}else{
ranks <- data.frame(Team = dat$Team[dat$Playoffs == 0 & dat$Season %in% season],
Season = dat$Season[dat$Playoffs == 0 & dat$Season %in% season],
Week = dat$Week[dat$Playoffs == 0 & dat$Season %in% season],
sapply(dat %>% filter(Playoffs == 0 & Season %in% season) %>%
select(R:SV), rank),
sapply(dat %>% filter(Playoffs == 0 & Season %in% season) %>%
select(ERA, WHIP), function(x) nrow(dat[dat$Playoffs == as.numeric(playoffs) & dat$Season %in% season,]) + 1 - rank(x)))
ranks[['Score']] <- apply(ranks %>% select(R:WHIP), 1, sum)
return(ranks %>%
arrange_(ifelse(best, 'desc(Score)', 'Score')) %>%
filter(row_number() <= numshow) %>%
select(Team, Season, Week, Score) %>%
left_join(dat %>%
select(Team, Season, Week, Owner = TeamOwner, R:WHIP),
c('Team', 'Season', 'Week')) %>%
mutate(WinPct = Score / (nrow(dat[dat$Playoffs == as.numeric(playoffs) & dat$Season %in% season,]) * 12)) %>%
select(-Score))
}
} else{
if(playoffs){
return(dat %>%
filter(Playoffs == 1 & Season %in% season) %>%
select_(.dots = c('Team', 'Season', 'Week', statcat)) %>%
arrange_(ifelse((statcat %in% c('WHIP', 'ERA') & best) | (!(statcat %in% c('WHIP', 'ERA')) & !best), statcat, paste0('desc(', statcat, ')'))) %>%
filter(row_number() <= numshow))
}else{
return(dat %>%
filter(Playoffs == 0 & Season %in% season) %>%
select_(.dots = c('Team', 'Season', 'Week', statcat)) %>%
arrange_(ifelse((statcat %in% c('WHIP', 'ERA') & best) | (!(statcat %in% c('WHIP', 'ERA')) & !best), statcat, paste0('desc(', statcat, ')'))) %>%
filter(row_number() <= numshow))
}
}
}
}
# records.func(dat = dat,
# weekly = T,
# Season = 2016,
# statcat = 'R',
# playoffs = F,
# best = T,
# numshow = 5)