The way ROTO scoring works is that the team with the most stats in a certain category is awarded the most ROTO points. For example in a league of 10 teams, the team with the most HRs is awarded 10 ROTO points, the team with the 2nd most HRs is awarded 9 ROTO points all the way down to the team with the least amount of HRs who is awarded 1 ROTO point. If there is more than one team with the same amount of stats in one category then those team split the total points that would have been allocated to those teams. For example, if four people tie for the most HRs, then the 10 + 9 + 8 + 7 ROTO points are devided up evenly between those four teams. So each team recieves (10+9+8+7)/4 = 8.5 ROTO points.

For categories like ERA and WHIP the most ROTO points are allocated to the team with lowest value. So in a league with 10 teams the team with lowest ERA is awarded 10 ROTO points.

library(tidyverse)
all_week_stats <- read_delim("/Users/noahknoblauch/Dropbox/Baseball/all_week_stats.txt",delim="\t",guess_max=10000)

all_stats_hits_innings <- all_week_stats


all_week_stats <- all_week_stats %>%
  filter(Stat != "Hits / At Bats", Stat != "IP")%>%
  mutate(stat_value=as.numeric(stat_value)) %>%
  group_by(Stat,week)%>%
  mutate(roto_points=ifelse(Stat =="WHIP"|Stat=="ERA",rank(-stat_value),rank(stat_value)))



hits_ab <- all_stats_hits_innings%>%
  filter(Stat=="Hits / At Bats")

hits_ab <-separate(hits_ab,stat_value,c("hits","at_bats"),sep="/")%>%
  transform(hits=as.numeric(hits),at_bats=as.numeric(at_bats))

batting_avg <- hits_ab%>%
  group_by(real_name)%>%
  summarise(hits=sum(hits),At_bats=sum(at_bats))%>%
  mutate(AVG=hits/At_bats)


Earned_runs <- all_stats_hits_innings%>%
  filter(Stat=="IP"|Stat=="ERA"|Stat=="WHIP")%>%
  transform(stat_value=as.numeric(stat_value))%>%
  spread(Stat,stat_value)

names(Earned_runs)[2] <- "Manager"

Earned_runs <- Earned_runs%>%
  group_by(week)%>%
  mutate(runs=(ERA*IP)/9)%>%
  mutate(walks_hits=IP*WHIP)%>%
  group_by(Manager)%>%
  summarise(Total_runs=sum(runs),Total_walk_hits=sum(walks_hits),IP=sum(IP))
  

ERA_WHIP <- Earned_runs%>%
  mutate(WHIP=Total_walk_hits/IP,ERA=(Total_runs/IP)*9)%>%
  select(-Total_walk_hits,-Total_runs,-IP)

gathered_ERA_WHIP <- gather(ERA_WHIP,Stat,value,-Manager)%>%
  group_by(Stat)%>%
  mutate(roto=rank(-value))


batting_roto <- all_week_stats%>%
  filter(Stat!="AVG",Stat!="ERA",Stat!="WHIP")
names(batting_roto)[3]<-"Manager"

batting_roto<- batting_roto%>%
  group_by(Manager,Stat)%>%
  summarise(value=sum(stat_value))%>%
  group_by(Stat)%>%
  mutate(roto=rank(value))

final_roto <- rbind(gathered_ERA_WHIP,batting_roto)

roto_season_scoreboard <- final_roto %>%
  group_by(Manager)%>%
  summarise(roto=sum(roto))%>%
  arrange(desc(roto))%>%ungroup()

summed_weekly_roto <- all_week_stats %>%
  group_by(real_name,week) %>%
  summarise(total=sum(roto_points)) %>%
  group_by(real_name)%>%
  summarise(total=sum(total))%>%
  arrange(desc(total))
regular_season_stats <-all_week_stats%>%
  filter(week !=22,week!=23,week!=24)

Scoreboard <- regular_season_stats%>%
  group_by(real_name,week)%>%
  summarise(ROTO=sum(roto_points))%>%
  arrange(week)

winners <- Scoreboard %>%
  group_by(week)%>%
  filter(ROTO==max(ROTO))
  
winner_total <- winners%>%
  group_by(real_name)%>% summarise(weeks_won = n())%>% arrange(desc(weeks_won))
  
week_value<-list()

for (i in 1:21){
  week<- all_week_stats%>% filter(week==i)%>%select(real_name,Stat,stat_value)
  df <- spread(week,Stat,stat_value)

  week_roto <-Scoreboard %>%filter(week==i)%>%select(ROTO,real_name)
  week_stats <- inner_join(df,week_roto,by="real_name")
  week_value[[i]] <-week_stats
}

This R Markdown site was created with workflowr