-
Notifications
You must be signed in to change notification settings - Fork 0
/
Youngest and Oldest to Play and Score for a Team.R
123 lines (85 loc) · 4.41 KB
/
Youngest and Oldest to Play and Score for a 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Youngest and Oldest to Play and Score for a Team
#
# Who is the youngest player to play for a team?
# Who is the oldest player to play for a team?
# Who is the youngest player to score a goal for a team?
# Who is the oldest player to score a goal 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");
skaterStats$wins <- NA
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");
firstGame <- playerStats %>%
group_by(playerId) %>%
summarize(gameId = min(gameId))
firstGoal <- playerStats %>%
filter(goals > 0) %>%
group_by(playerId) %>%
summarize(gameId = min(gameId))
firstWin <- playerStats %>%
filter(wins > 0) %>%
group_by(playerId) %>%
summarize(gameId = min(gameId))
lastGame <- playerStats %>%
group_by(playerId) %>%
summarize(gameId = max(gameId))
lastGoal <- playerStats %>%
filter(goals > 0) %>%
group_by(playerId) %>%
summarize(gameId = max(gameId))
lastWin <- playerStats %>%
filter(wins > 0) %>%
group_by(playerId) %>%
summarize(gameId = max(gameId))
## ALL PLAYERS
# Youngest players to play a game
firstGame <- merge(firstGame, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, goals) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( firstGame %>% arrange(ageDays) , 10 )
# Youngest players to score a goal
firstGoal <- merge(firstGoal, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, goals) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( firstGoal %>% arrange(ageDays) , 10 )
# Oldest players to play a game
lastGame <- merge(lastGame, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, goals) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( lastGame %>% arrange(desc(ageDays)) , 10 )
# Oldest players to score a goal
lastGoal <- merge(lastGoal, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, goals) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( lastGoal %>% arrange(desc(ageDays)) , 10 )
### GOALIES ONLY
# Youngest goalies to play a game
head( firstGame %>% filter(playerPositionCode == "G") %>% arrange(ageDays) , 10 )
# Youngest goalies to win a game
firstWin <- merge(firstWin, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, wins) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( firstWin %>% arrange(ageDays) , 10 )
# Oldest goalies to play a game
head( lastGame %>% filter(playerPositionCode == "G") %>% arrange(desc(ageDays)) , 10 )
# Oldest goalies to win a game
lastWin <- merge(lastWin, playerStats) %>%
select(gameId, gameDate, playerId, playerName, playerBirthDate, playerPositionCode, opponentTeamAbbrev, wins) %>%
mutate(ageDays = gameDate - playerBirthDate) %>%
mutate(ageYears = computeYearsAndDaysNumber(playerBirthDate, gameDate))
head( lastWin %>% arrange(desc(ageDays)) , 10 )