Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active August 29, 2016 13:01
Show Gist options
  • Save batpigandme/e743bb01227bd43eddf7 to your computer and use it in GitHub Desktop.
Save batpigandme/e743bb01227bd43eddf7 to your computer and use it in GitHub Desktop.
Get and load team game logs from Stattleship API. Assumes you've retrieved teams, if not, see: https://gist.github.com/batpigandme/d06f279369546dbfe462
## load the stattleshipR and dplyr packages
library(stattleshipR)
library(dplyr)
## set params for team_game_logs
sport <- "basketball"
league <- "nba"
ep <- "team_game_logs"
## leave q_body empty to get all
## set season_id
q_body <- list(season_id="nba-2015-2016")
## get the team game logs
nba_team_gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=TRUE)
## bind together data into a data table
tgls <- lapply(nba_team_gls, function(x) x$team_game_logs)
team_game_logs <- do.call('rbind', tgls)
team_game_logs <- rbindlist(tgls)
## team game log data cleanup
team_game_logs$coach_technical_fouls[which(is.na(team_game_logs$coach_technical_fouls))] <- 0
team_game_logs$points_biggest_lead[which(is.na(team_game_logs$points_biggest_lead))] <- 0
team_game_logs$technical_fouls[which(is.na(team_game_logs$technical_fouls))] <- 0
## add team details from nba_teams
team_game_logs$team_slug <- nba_teams[match(team_game_logs$team_id, nba_teams$id),]$slug
team_game_logs$opponent_slug <- nba_teams[match(team_game_logs$opponent_id, nba_teams$id),]$slug
## add game details
games <- lapply(nba_team_gls, function(x) x$games)
the_games <- do.call('rbind', games)
the_games <- the_games[match(team_game_logs$game_id, the_games$id),]
team_game_logs$scoreline <- the_games[match(team_game_logs$game_id, the_games$id),]$scoreline
team_game_logs$winning_team_id <- the_games[match(team_game_logs$game_id, the_games$id),]$winning_team_id
team_game_logs$score_differential <- the_games[match(team_game_logs$game_id, the_games$id),]$score_differential
team_game_logs$game_started_at <- the_games[match(team_game_logs$game_id, the_games$id),]$started_at
team_game_logs$game_ended_at <- the_games[match(team_game_logs$game_id, the_games$id),]$ended_at
team_game_logs$game_slug <- the_games[match(team_game_logs$game_id, the_games$id),]$slug
## optional add date of game as date
team_game_logs$date<-as.Date(team_game_logs$game_started_at)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment