-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Gap in Scoring for Team.R
67 lines (48 loc) · 2.51 KB
/
Longest Gap in Scoring for Team.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Longest Gap in Scoring for a Team
#
# Sometimes players will go a really long time between scoring goals for a team
source("nhl-r.R")
teamId <- 18; # 18 = Nashville Predators on nhl.com/stats (call "getLeagueHistorySummary()" to look up the ID for another team)
### Get all player stats in each game
skaterStats <- getPlayerGameDetails(teamId, "skaters");
goalieStats <- getPlayerGameDetails(teamId, "goalies");
columnNames <- names(skaterStats)
columnNames <- columnNames[columnNames %in% names(goalieStats)]
# Combine skaters and goalies into a common dataset
playerStats <- rbind(
skaterStats %>% select_(.dots = columnNames),
goalieStats %>% select_(.dots = columnNames)
)
playerStats$playerBirthDate <- as.Date(playerStats$playerBirthDate);
playerStats$gameDate <- as.Date(playerStats$gameDate, tz = "America/Chicago");
# Add a column that tracks the number of games the player has played for the specified team
playerStats <- playerStats %>%
group_by(playerId) %>%
mutate(teamGameNo = dense_rank(gameDate))
# Add a flag to identify players who are currently on the team
currentSeasonId <- computeSeasonIdForDate();
currentPlayerIds <- unique(playerStats[playerStats$seasonId == currentSeasonId, ]$playerId);
playerStats$isCurrentPlayer <- playerStats$playerId %in% currentPlayerIds;
# For each player, add a column that shows when each player scored his previous goal for the specified team
goals <- playerStats %>%
select(playerId, playerName, playerBirthDate, gameDate, gameId, teamAbbrev, opponentTeamAbbrev, goals, teamGameNo, isCurrentPlayer) %>%
filter(goals > 0) %>%
group_by(playerId) %>%
mutate(lastGoal = lag(gameDate, order_by = gameDate)) %>%
arrange(playerId, gameDate)
# Quick sanity check
sum(goals$goals)
sum(getTeamSeasonSummaries(teamId)$goalsFor)
# Show the longest scoring droughts for the specified team
goals %>%
mutate(yearsSince = computeYearsAndDaysNumber(lastGoal, gameDate)) %>%
arrange(desc(yearsSince)) %>%
select(playerId, playerName, gameDate, teamAbbrev, opponentTeamAbbrev, goals, teamGameNo, lastGoal, yearsSince)
# Check all of the current players. If they scored today, where would that put them on the list?
goals %>%
filter(isCurrentPlayer) %>%
group_by(playerId) %>%
summarize(playerName = max(playerName), lastGoal = max(gameDate)) %>%
mutate(daysSince = Sys.Date() - lastGoal) %>%
mutate(yearsSince = computeYearsAndDaysNumber(lastGoal, Sys.Date())) %>%
arrange(desc(daysSince))