Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active January 29, 2016 17:12
Show Gist options
  • Save batpigandme/b13e8f5974f0aecff3b4 to your computer and use it in GitHub Desktop.
Save batpigandme/b13e8f5974f0aecff3b4 to your computer and use it in GitHub Desktop.
Match opponent leads to team game logs and select variables for new, focused data table, as well as a summary table for each team.
## set keys for the data table on game_slug and opponent_slug
team_game_logs <- setkey(team_game_logs, game_slug, opponent_slug)
## get opponent leads by matching on both key columns
team_game_logs$opp_biggest_lead <- team_game_logs[. (team_game_logs$game_slug, team_game_logs$opponent_slug)]$points_biggest_lead
## set keys for the data table on game_slug and opponent_slug
team_game_logs <- setkey(team_game_logs, game_slug, opponent_slug)
## get opponent leads by matching on both key columns
team_game_logs$opp_biggest_lead <- team_game_logs[. (team_game_logs$game_slug, team_game_logs$opponent_slug)]$points_biggest_lead
## select variables for new table
## rename so you don't lose original data
teamgame_point_leads <- select(team_game_logs,
id,
game_slug,
team_slug,
team_outcome,
points_biggest_lead,
opponent_slug,
opp_biggest_lead,
scoreline,
score_differential,
date)
## add final point differential
teamgame_point_leads$WL <- ifelse(teamgame_point_leads$team_outcome == "win", 1, -1)
teamgame_point_leads$final_point_diff <- (teamgame_point_leads$score_differential * teamgame_point_leads$WL)
## make a summary table for teamgame leads by team
## group records by team_slug
summary_teamgame_leads <- group_by(teamgame_point_leads, team_slug)
## select summary functions
summary_teamgame_leads <- summarise(summary_teamgame_leads,
mean_biggest_lead = mean(points_biggest_lead, na.rm = TRUE),
max_biggest_lead = max(points_biggest_lead, na.rm = TRUE),
median_biggest_lead = median(points_biggest_lead, na.rm = TRUE),
opp_mean_biggest_lead = mean(opp_biggest_lead, na.rm = TRUE),
opp_max_biggest_lead = max(opp_biggest_lead, na.rm = TRUE),
opp_median_biggest_lead = median(opp_biggest_lead, na.rm = TRUE)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment