View Single Post
Old 04-06-2019, 03:46 PM   #677
stealofhome
Hall Of Famer
 
stealofhome's Avatar
 
Join Date: Apr 2014
Posts: 2,282
Blog Entries: 1
High School MVP and POY

Here's the code for selecting the MVP and POY for the high school leagues.

Code:
library(readr)
library(dplyr)
leagues <- read_csv("Out of the Park Developments/OOTP Baseball 19/saved_games/OOTPLeagueReborn.lg/import_export/general/leagues.csv")

#import data (players_basic, players_value, players_career_pitching_stats, players_career_batting_stats)
players <- read_csv("Out of the Park Developments/OOTP Baseball 19/saved_games/OOTPLeagueReborn.lg/import_export/csv/players.csv")
players <- unite(players, name, c("first_name","last_name"), sep = " ")
players_career_pitching_stats <- read_csv("Out of the Park Developments/OOTP Baseball 19/saved_games/OOTPLeagueReborn.lg/import_export/csv/players_career_pitching_stats.csv")
players_value <- read_csv("Out of the Park Developments/OOTP Baseball 19/saved_games/OOTPLeagueReborn.lg/import_export/csv/players_value.csv")
players_career_batting_stats <- read_csv("Out of the Park Developments/OOTP Baseball 19/saved_games/OOTPLeagueReborn.lg/import_export/csv/players_career_batting_stats.csv")

#Reduce pitching data to HS in current year and find rank
phs <- players_career_pitching_stats %>% 
  select(player_id, year, level_id, league_id, split_id, bf, gs, wpa, war) %>% 
  filter(level_id=='11' & split_id=='1'& year==max(year)) %>%
  merge(players[ , c("player_id", "name")], by = "player_id") %>% 
  merge(players_value[ , c("player_id", "pot")], by = "player_id") %>% 
  mutate(rank = 0.75*wpa+2*war+pot/30, position = "P") %>% 
  select(player_id, league_id, name, rank, position)

#Reduce hitting data to HS in current year and find rank
bhs <- players_career_batting_stats %>% 
  select(player_id, year, level_id, league_id, split_id, pa, wpa, war) %>% 
  filter(level_id=='11' & split_id=='1'& year==max(year)) %>%
  merge(players[ , c("player_id", "name")], by = "player_id") %>% 
  merge(players_value[ , c("player_id", "pot")], by = "player_id") %>% 
  mutate(rank = 0.75*wpa+2*war+pot/30, position = "H") %>% 
  select(player_id, league_id, name, rank, position)

#merge hitters and pitchers for MVP
hsmvp <- phs %>% 
  bind_rows(bhs) %>% 
  merge(leagues[ , c("league_id", "abbr")], by = "league_id") %>%
  arrange(abbr, desc(rank)) %>% 
  group_by(abbr) %>% 
  top_n(3, rank) %>% 
  select(name,position,abbr)

#POY award
hspoy <- phs %>% 
  merge(leagues[ , c("league_id", "abbr")], by = "league_id") %>%
  arrange(abbr, desc(rank)) %>% 
  group_by(abbr) %>% 
  top_n(3, rank) %>% 
  select(name,abbr)
stealofhome is offline   Reply With Quote