Btibert3
12/7/2015 - 9:00 PM

Page and get gamelogs

Page and get gamelogs

Quick playing with game logs.

Get and parse Brad Marchand Game logs

The function isn't born out yet, but let's see how many games we have.

player = list(player_id = "nhl-brad-marchand")
raw_response = queryAPI(TOKEN, ep="game_logs", query=player, verbose=T)

How many games and the number of results per page?

games_played = as.numeric(raw_response$response$headers$total)
# let the API tell us how many per page
rpp = as.numeric(raw_response$response$headers$`per-page`)
## how many pages do we need to get
num_pages = ceiling(games_played / rpp)

Hacky way now, but walk and get the data. Needs to be better than this.

## initialize a data frame
marchy_logs = data.frame()
marchy_games = data.frame()

## assumes that page=1 is the same as the default without page supplied
for (i in 1:num_pages) {
  tmp = queryAPI(TOKEN, ep = "game_logs", query=player, page=i)
  gamelog = tmp$game_log
  game = tmp$games
  marchy_logs = bind_rows(marchy_logs, gamelog)
  marchy_games = bind_rows(marchy_games, game)
}

What do we have

marchy_logs = as.data.frame(marchy_logs)
marchy_games = as.data.frame(marchy_games)
nrow(marchy_logs)
[1] 24
nrow(marchy_games)
[1] 24
head(marchy_logs)
                                                                        id
1 553a660e-e5c8-4bcc-9e7d-23e508897a1609c6fba9-2661-4879-b13e-94b3d7f1d692
2 ed39fe61-ac5e-4879-99bd-a2eace45610909c6fba9-2661-4879-b13e-94b3d7f1d692
3 930b7ec0-f028-442c-9982-d1be78cd69a809c6fba9-2661-4879-b13e-94b3d7f1d692
4 7d9dbcba-0408-4dce-bcf8-a4eb7c7ddabf09c6fba9-2661-4879-b13e-94b3d7f1d692
5 da96aad4-3b35-494d-8e43-dd60a263321b09c6fba9-2661-4879-b13e-94b3d7f1d692
6 31fa47b8-ce1c-46ee-aa35-008383c359e709c6fba9-2661-4879-b13e-94b3d7f1d692
            created_at           updated_at assists blocked_shots
1 2015-11-30T06:22:50Z 2015-11-30T06:23:13Z       1             1
2 2015-11-30T06:35:29Z 2015-11-30T06:35:49Z       0             0
3 2015-11-30T06:48:29Z 2015-11-30T06:48:50Z       0             0
4 2015-11-30T06:52:51Z 2015-11-30T06:53:13Z       0             0
5 2015-11-30T06:55:53Z 2015-11-30T17:24:25Z       1             0
6 2015-11-30T06:56:39Z 2015-11-30T06:57:01Z       0             1
  faceoffs_won faceoffs_lost faceoff_win_percentage goals goals_power_play
1            0             0                    0.0     0                0
2            0             0                    0.0     0                0
3            0             0                    0.0     0                0
4            1             0                    1.0     2                1
5            0             0                    0.0     0                0
6            0             0                    0.0     0                0
  goals_short_handed hits penalty_minutes plus_minus points
1                  0    1               2          1      1
2                  0    1               0          0      0
3                  0    3               2          0      0
4                  0    2              15          1      2
5                  0    0               0          1      1
6                  0    1               2         -1      0
  points_power_play shifts shooting_pct shots takeaways time_on_ice_secs
1                 0     25          0.0     3         0             1173
2                 0     23          0.0     4         0              991
3                 0     23          0.0     3         0             1093
4                 1     25         66.7     3         0             1073
5                 0     19          0.0     4         0              985
6                 0     22          0.0     3         0             1039
                               game_id
1 553a660e-e5c8-4bcc-9e7d-23e508897a16
2 ed39fe61-ac5e-4879-99bd-a2eace456109
3 930b7ec0-f028-442c-9982-d1be78cd69a8
4 7d9dbcba-0408-4dce-bcf8-a4eb7c7ddabf
5 da96aad4-3b35-494d-8e43-dd60a263321b
6 31fa47b8-ce1c-46ee-aa35-008383c359e7
                             player_id
1 09c6fba9-2661-4879-b13e-94b3d7f1d692
2 09c6fba9-2661-4879-b13e-94b3d7f1d692
3 09c6fba9-2661-4879-b13e-94b3d7f1d692
4 09c6fba9-2661-4879-b13e-94b3d7f1d692
5 09c6fba9-2661-4879-b13e-94b3d7f1d692
6 09c6fba9-2661-4879-b13e-94b3d7f1d692
                               team_id
1 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
2 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
3 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
4 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
5 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
6 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
head(marchy_games)
                                    id           created_at
1 553a660e-e5c8-4bcc-9e7d-23e508897a16 2015-11-30T06:22:50Z
2 ed39fe61-ac5e-4879-99bd-a2eace456109 2015-11-30T06:35:29Z
3 930b7ec0-f028-442c-9982-d1be78cd69a8 2015-11-30T06:48:29Z
4 7d9dbcba-0408-4dce-bcf8-a4eb7c7ddabf 2015-11-30T06:52:51Z
5 da96aad4-3b35-494d-8e43-dd60a263321b 2015-11-30T06:55:53Z
6 31fa47b8-ce1c-46ee-aa35-008383c359e7 2015-11-30T06:56:39Z
            updated_at at_neutral_site attendance away_team_outcome
1 2015-11-30T06:23:13Z           FALSE      13113               win
2 2015-11-30T06:35:49Z           FALSE      17565               win
3 2015-11-30T06:48:50Z           FALSE      17565               win
4 2015-11-30T06:53:13Z           FALSE      14112               win
5 2015-11-30T17:24:25Z           FALSE      13197               win
6 2015-11-30T06:57:01Z           FALSE      18506              loss
  away_team_score daytime duration             ended_at home_team_outcome
1               5   FALSE     9900 2015-10-24T02:08:50Z              loss
2               6   FALSE     8760 2015-10-09T01:38:49Z              loss
3               4   FALSE    10200 2015-10-11T02:05:09Z              loss
4               3   FALSE     9000 2015-10-31T02:13:31Z              loss
5               2   FALSE     9000 2015-11-09T00:40:20Z              loss
6               1   FALSE     9300 2015-11-06T02:44:06Z               win
  home_team_score       interval interval_number interval_type
1               3 Regular Season                 regularseason
2               2 Regular Season                 regularseason
3               2 Regular Season                 regularseason
4               1 Regular Season                 regularseason
5               1 Regular Season                 regularseason
6               4 Regular Season                 regularseason
                label                                             name
1 Bruins vs Islanders  Bruins vs Islanders October 23, 2015 at 11:30pm
2      Jets vs Bruins       Jets vs Bruins October  8, 2015 at 11:00pm
3 Canadiens vs Bruins  Canadiens vs Bruins October 10, 2015 at 11:00pm
4  Bruins vs Panthers   Bruins vs Panthers October 30, 2015 at 11:30pm
5 Bruins vs Islanders Bruins vs Islanders November  8, 2015 at 10:00pm
6  Bruins vs Capitals  Bruins vs Capitals November  6, 2015 at 12:00am
                    on score score_differential              scoreline
1  on October 23, 2015   5-3                  2 Bruins 5 - Islanders 3
2  on October  8, 2015   6-2                  4      Jets 6 - Bruins 2
3  on October 10, 2015   4-2                  2 Canadiens 4 - Bruins 2
4  on October 30, 2015   3-1                  2  Bruins 3 - Panthers 1
5 on November  8, 2015   2-1                  1 Bruins 2 - Islanders 1
6 on November  6, 2015   1-4                  3  Bruins 1 - Capitals 4
                                   slug           started_at temperature
1 nhl-2015-2016-bos-nyi-2015-10-23-2330 2015-10-23T23:30:00Z          NA
2  nhl-2015-2016-win-bos-2015-10-8-2300 2015-10-08T23:00:00Z          NA
3 nhl-2015-2016-mon-bos-2015-10-10-2300 2015-10-10T23:00:00Z          NA
4 nhl-2015-2016-bos-fla-2015-10-30-2330 2015-10-30T23:30:00Z          NA
5  nhl-2015-2016-bos-nyi-2015-11-8-2200 2015-11-08T22:00:00Z          NA
6  nhl-2015-2016-bos-was-2015-11-6-0000 2015-11-06T00:00:00Z          NA
  temperature_unit  timestamp               title weather_conditions
1               NA 1445643000 Bruins vs Islanders                 NA
2               NA 1444345200      Jets vs Bruins                 NA
3               NA 1444518000 Canadiens vs Bruins                 NA
4               NA 1446247800  Bruins vs Panthers                 NA
5               NA 1447020000 Bruins vs Islanders                 NA
6               NA 1446768000  Bruins vs Capitals                 NA
  wind_direction wind_speed wind_speed_unit
1             NA         NA              NA
2             NA         NA              NA
3             NA         NA              NA
4             NA         NA              NA
5             NA         NA              NA
6             NA         NA              NA
                          home_team_id
1 1f2dbc61-cced-4d84-9ee9-81bae4c9c70f
2 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
3 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
4 7e5c2566-c3cc-4b2e-98e6-d8bce74b7fbc
5 1f2dbc61-cced-4d84-9ee9-81bae4c9c70f
6 54276c06-42a0-4649-838c-a462451d54c8
                          away_team_id
1 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
2 157c2242-afa5-499d-906d-6a992927e58d
3 80d99c5a-882e-4296-ae9d-048d2830b154
4 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
5 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
6 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
                       winning_team_id
1 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
2 157c2242-afa5-499d-906d-6a992927e58d
3 80d99c5a-882e-4296-ae9d-048d2830b154
4 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
5 e9d7bcb5-6723-4ca9-b1d9-1ccd026be1cb
6 54276c06-42a0-4649-838c-a462451d54c8
                             season_id
1 301759de-976d-4daa-a194-cacea1dc02ca
2 301759de-976d-4daa-a194-cacea1dc02ca
3 301759de-976d-4daa-a194-cacea1dc02ca
4 301759de-976d-4daa-a194-cacea1dc02ca
5 301759de-976d-4daa-a194-cacea1dc02ca
6 301759de-976d-4daa-a194-cacea1dc02ca

Game types

table(marchy_games$interval_type)

    preseason regularseason 
            2            22 

Now let's explore the games