Skip to content

Instantly share code, notes, and snippets.

@sangamc
Forked from tcash21/stattleship_mlb_demo.R
Created August 22, 2016 04:32
Show Gist options
  • Save sangamc/2e3d16faabd3736a6d46200210c53f56 to your computer and use it in GitHub Desktop.
Save sangamc/2e3d16faabd3736a6d46200210c53f56 to your computer and use it in GitHub Desktop.
install.packages("devtools")
devtools::install_github("stattleship/stattleship-r")
install.packages('dplyr')
install.packages('ggplot2')
## Load the stattleshipR package
library(stattleshipR)
library(dplyr)
library(ggplot2)
set_token("your-api-token")
sport <- 'baseball'
league <- 'mlb'
ep <- 'game_logs'
q_body <- list(team_id='mlb-bos', status='ended', interval_type='regularseason')
gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, walk=TRUE)
game_logs<-do.call('rbind', lapply(gls, function(x) x$game_logs))
sport <- 'baseball'
league <- 'mlb'
ep <- 'players'
q_body <- list(team_id='mlb-bos')
pls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, walk=TRUE)
players<-do.call('rbind', lapply(pls, function(x) x$players))
colnames(players)[1] <- 'player_id'
game_logs <- merge(players, game_logs, by='player_id')
stats <-
game_logs %>%
filter(game_played == TRUE) %>%
group_by(name) %>%
summarise(totalRuns = sum(runs), meanBA = mean(batting_average), totalBases=sum(total_bases), salary=max(salary))
ggplot(stats, aes(x=totalRuns, y=meanBA, size=totalBases, label=name, color=salary)) + geom_text()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment