Last active
August 29, 2016 13:01
-
-
Save batpigandme/6f089e48f89c2dce0f1f to your computer and use it in GitHub Desktop.
Retrieve nba-stephen-curry game logs from stattleship API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## install and load the stattleshipR package | |
devtools::install_github("stattleship/stattleship-r") | |
library(stattleshipR) | |
## optional: load dplyr | |
library(dplyr) | |
## get your API token stattleship.com | |
## set API token | |
set_token('YOUR API TOKEN') | |
## set params | |
sport <- "basketball" | |
league <- "nba" | |
ep <- "game_logs" | |
q_body <- list(player_id="nba-stephen-curry", season_id="nba-2015-2016") | |
steph_gls <- ss_get_result(sport=sport, league=league, ep=ep, query=q_body, version=1, verbose=TRUE, walk=TRUE) | |
## get gl and game info from list | |
sgls <- lapply(steph_gls, function(x) x$game_logs) | |
games <- lapply(steph_gls, function(x) x$games) | |
## data into data tables | |
the_games <- rbindlist(games) | |
steph_gls <- rbindlist(sgls) | |
## filter for just games played | |
steph_gls <- filter(steph_gls, game_played == "TRUE") | |
## data cleanup | |
i <- which(steph_gls$game_played == TRUE) | |
steph_gls <- steph_gls[i,] | |
steph_gls$game_started[which(is.na(steph_gls$game_started))] <- FALSE | |
steph_gls$field_goals_made[which(is.na(steph_gls$field_goals_made))] <- 0 | |
steph_gls$assists[which(is.na(steph_gls$assists))] <- 0 | |
steph_gls$rebounds_offensive[which(is.na(steph_gls$rebounds_offensive))] <- 0 | |
steph_gls$rebounds_defensive[which(is.na(steph_gls$rebounds_defensive))] <- 0 | |
steph_gls$field_goals_attempted[which(is.na(steph_gls$field_goals_attempted))] <- 0 | |
steph_gls$field_goals_made[which(is.na(steph_gls$field_goals_made))] <- 0 | |
steph_gls$turnovers[which(is.na(steph_gls$turnovers))] <- 0 | |
steph_gls$points[which(is.na(steph_gls$points))] <- 0 | |
steph_gls$free_throws_attempted[which(is.na(steph_gls$free_throws_attempted))] <- 0 | |
steph_gls$free_throws_made[which(is.na(steph_gls$free_throws_made))] <- 0 | |
steph_gls$three_pointers_attempted[which(is.na(steph_gls$three_pointers_attempted))] <- 0 | |
steph_gls$three_pointers_made[which(is.na(steph_gls$three_pointers_made))] <- 0 | |
steph_gls$three_pointers_pct <- as.numeric(steph_gls$three_pointers_pct) | |
steph_gls$steals[which(is.na(steph_gls$steals))] <- 0 | |
steph_gls$blocks[which(is.na(steph_gls$blocks))] <- 0 | |
steph_gls$personal_fouls[which(is.na(steph_gls$personal_fouls))] <- 0 | |
steph_gls$technical_fouls[which(is.na(steph_gls$technical_fouls))] <- 0 | |
steph_gls$disqualifications[which(is.na(steph_gls$disqualifications))] <- 0 | |
steph_gls$plus_minus[which(is.na(steph_gls$plus_minus))] <- 0 | |
steph_gls$court <- ifelse(steph_gls$team_outcome == steph_gls$home_team_outcome, "home", "away") | |
## add game detail | |
steph_gls$scoreline <- the_games[match(steph_gls$game_id, the_games$id),]$scoreline | |
steph_gls$winning_team_id <- the_games[match(steph_gls$game_id, the_games$id),]$winning_team_id | |
steph_gls$score_differential <- the_games[match(steph_gls$game_id, the_games$id),]$score_differential | |
steph_gls$game_started_at <- the_games[match(steph_gls$game_id, the_games$id),]$started_at | |
steph_gls$game_ended_at <- the_games[match(steph_gls$game_id, the_games$id),]$ended_at | |
steph_gls$game_slug <- the_games[match(steph_gls$game_id, the_games$id),]$slug | |
## if desired write out csv | |
write.csv(steph_gls, file="steph_gls.csv", row.names = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment