Introduction to ballr

Ryan Elmore

2018-10-31

Introduction

Welcome to the ballr [baw-ler], as in baller1. This is the R resource for your basketball-reference.com needs.

library(ballr)
library(magrittr)
library(ggplot2)
library(janitor)
library(scales)

Example 1

Current standings

standings <- NBAStandingsByDate() # "YEAR-MO-DY"
standings
## $East
##      eastern_conference  w  l w_lpercent gb pw pl  ps_g  pa_g
## 1      Toronto Raptors* 59 23      0.720  — 60 22 111.7 103.9
## 2       Boston Celtics* 55 27      0.671  4 51 31 104.0 100.4
## 3   Philadelphia 76ers* 52 30      0.634  7 53 29 109.8 105.3
## 4  Cleveland Cavaliers* 50 32      0.610  9 43 39 110.9 109.9
## 5       Indiana Pacers* 48 34      0.585 11 45 37 105.6 104.2
## 6           Miami Heat* 44 38      0.537 15 42 40 103.4 102.9
## 7      Milwaukee Bucks* 44 38      0.537 15 40 42 106.5 106.8
## 8   Washington Wizards* 43 39      0.524 16 43 39 106.6 106.0
## 9       Detroit Pistons 39 43      0.476 20 41 41 103.8 103.9
## 10    Charlotte Hornets 36 46      0.439 23 42 40 108.2 108.0
## 11      New York Knicks 29 53      0.354 30 32 50 104.5 108.0
## 12        Brooklyn Nets 28 54      0.341 31 31 51 106.6 110.3
## 13        Chicago Bulls 27 55      0.329 32 23 59 102.9 110.0
## 14        Orlando Magic 25 57      0.305 34 28 54 103.4 108.2
## 15        Atlanta Hawks 24 58      0.293 35 27 55 103.4 108.8
## 
## $West
##         western_conference  w  l w_lpercent gb pw pl  ps_g  pa_g
## 1         Houston Rockets* 65 17      0.793  — 61 21 112.4 103.9
## 2   Golden State Warriors* 58 24      0.707  7 56 26 113.5 107.5
## 3  Portland Trail Blazers* 49 33      0.598 16 48 34 105.6 103.0
## 4    New Orleans Pelicans* 48 34      0.585 17 44 38 111.7 110.4
## 5   Oklahoma City Thunder* 48 34      0.585 17 50 32 107.9 104.4
## 6               Utah Jazz* 48 34      0.585 17 53 29 104.1  99.8
## 7       San Antonio Spurs* 47 35      0.573 18 49 33 102.7  99.8
## 8  Minnesota Timberwolves* 47 35      0.573 18 47 35 109.5 107.3
## 9           Denver Nuggets 46 36      0.561 19 45 37 110.0 108.5
## 10    Los Angeles Clippers 42 40      0.512 23 41 41 109.0 109.0
## 11      Los Angeles Lakers 35 47      0.427 30 37 45 108.1 109.6
## 12        Sacramento Kings 27 55      0.329 38 23 59  98.8 105.8
## 13        Dallas Mavericks 24 58      0.293 41 33 49 102.3 105.4
## 14       Memphis Grizzlies 22 60      0.268 43 25 57  99.3 105.5
## 15            Phoenix Suns 21 61      0.256 44 19 63 103.9 113.3

Standings on an arbitrary date

standings <- NBAStandingsByDate("2015-12-31")
standings
## $East
##      eastern_conference  w  l w_lpercent   gb pw pl  ps_g  pa_g
## 1  Cleveland Cavaliers* 21  9      0.700    — 20 10  99.7  95.1
## 2        Atlanta Hawks* 21 13      0.618    2 19 15 102.0 100.1
## 3      Toronto Raptors* 20 13      0.606  2.5 20 13  99.8  96.4
## 4         Chicago Bulls 18 12      0.600    3 16 14 101.1 100.0
## 5         Orlando Magic 19 13      0.594    3 19 13 101.0  98.4
## 6           Miami Heat* 18 13      0.581  3.5 17 14  97.0  95.5
## 7       Indiana Pacers* 18 13      0.581  3.5 20 11 102.3  98.5
## 8       Boston Celtics* 18 14      0.563    4 20 12 103.1  99.1
## 9    Charlotte Hornets* 17 14      0.548  4.5 18 13 102.5  99.7
## 10     Detroit Pistons* 17 15      0.531    5 17 15 101.0 100.2
## 11      New York Knicks 15 18      0.455  7.5 15 18  98.0  99.5
## 12   Washington Wizards 14 16      0.467    7 12 18 101.5 104.4
## 13      Milwaukee Bucks 12 21      0.364 10.5 10 23  97.1 103.2
## 14        Brooklyn Nets  9 23      0.281   13  9 23  97.1 103.4
## 15   Philadelphia 76ers  3 31      0.088   20  5 29  92.5 104.4
## 
## $West
##         western_conference  w  l w_lpercent   gb pw pl  ps_g  pa_g
## 1   Golden State Warriors* 29  2      0.935    — 26  5 114.1 101.8
## 2       San Antonio Spurs* 28  6      0.824  2.5 30  4 102.0  88.6
## 3   Oklahoma City Thunder* 22 10      0.688  7.5 24  8 108.6 100.4
## 4    Los Angeles Clippers* 20 13      0.606   10 19 14 103.1 100.9
## 5        Dallas Mavericks* 19 13      0.594 10.5 18 14 102.3 100.8
## 6       Memphis Grizzlies* 18 16      0.529 12.5 13 21  96.4  99.4
## 7         Houston Rockets* 16 17      0.485   14 15 18 104.1 105.5
## 8  Portland Trail Blazers* 14 20      0.412 16.5 16 18 101.3 102.0
## 9                Utah Jazz 13 17      0.433 15.5 14 16  96.6  97.3
## 10  Minnesota Timberwolves 12 20      0.375 17.5 14 18 100.4 102.6
## 11        Sacramento Kings 12 20      0.375 17.5 13 19 104.2 107.3
## 12          Denver Nuggets 12 21      0.364   18 11 22  98.9 103.8
## 13            Phoenix Suns 12 22      0.353 18.5 14 20 102.7 105.4
## 14    New Orleans Pelicans 10 21      0.323   19 11 20 102.1 107.0
## 15      Los Angeles Lakers  6 27      0.182   24  6 27  96.8 107.2

Example 2

players <- NBAPerGameStatistics()
players
## # A tibble: 664 x 31
##       rk player pos     age tm        g    gs    mp    fg   fga fgpercent
##    <dbl> <chr>  <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>     <dbl>
##  1     1 Alex … SG       24 OKC      75     8  15.1   1.5   3.9     0.395
##  2     2 Quinc… PF       27 BRK      70     8  19.4   1.9   5.2     0.356
##  3     3 Steve… C        24 OKC      76    76  32.7   5.9   9.4     0.629
##  4     4 Bam A… C        20 MIA      69    19  19.8   2.5   4.9     0.512
##  5     5 Arron… SG       32 ORL      53     3  12.9   1.2   3.1     0.401
##  6     6 Cole … C        29 MIN      21     0   2.3   0.2   0.7     0.333
##  7     7 LaMar… C        32 SAS      75    75  33.5   9.2  18       0.51 
##  8     8 Jarre… C        19 BRK      72    31  20     3.3   5.5     0.589
##  9     9 Kadee… PG       25 BOS      18     1   5.9   0.3   1.2     0.273
## 10    10 Tony … SF       36 NOP      22     0  12.4   2     4.1     0.484
## # ... with 654 more rows, and 20 more variables: x3p <dbl>, x3pa <dbl>,
## #   x3ppercent <dbl>, x2p <dbl>, x2pa <dbl>, x2ppercent <dbl>,
## #   efgpercent <dbl>, ft <dbl>, fta <dbl>, ftpercent <dbl>, orb <dbl>,
## #   drb <dbl>, trb <dbl>, ast <dbl>, stl <dbl>, blk <dbl>, tov <dbl>,
## #   pf <dbl>, ps_g <dbl>, link <chr>

Example 3

players <- NBAPerGameStatistics(season = 2017)
players %>%
  dplyr::filter(mp > 20, pos %in% c("SF")) %>%
  dplyr::select(player, link) %>%
  dplyr::distinct()
## # A tibble: 51 x 2
##    player                link                     
##    <chr>                 <chr>                    
##  1 Justin Anderson       /players/a/anderju01.html
##  2 Giannis Antetokounmpo /players/a/antetgi01.html
##  3 Carmelo Anthony       /players/a/anthoca01.html
##  4 Trevor Ariza          /players/a/arizatr01.html
##  5 Matt Barnes           /players/b/barnema02.html
##  6 Kent Bazemore         /players/b/bazemke01.html
##  7 Bojan Bogdanovic      /players/b/bogdabo02.html
##  8 Jimmy Butler          /players/b/butleji01.html
##  9 DeMarre Carroll       /players/c/carrode01.html
## 10 Vince Carter          /players/c/cartevi01.html
## # ... with 41 more rows

Example 4

players <- NBAPerGameStatisticsPer36Min(season = 2017)
players
## # A tibble: 595 x 30
##       rk player pos     age tm        g    gs    mp    fg   fga fgpercent
##    <dbl> <chr>  <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>     <dbl>
##  1     1 Alex … SG       23 OKC      68     6  1055   4.6  11.6     0.393
##  2     2 Quinc… PF       26 TOT      38     1   558   4.5  11       0.412
##  3     2 Quinc… PF       26 DAL       6     0    48   3.7  12.7     0.294
##  4     2 Quinc… PF       26 BRK      32     1   510   4.6  10.8     0.425
##  5     3 Steve… C        23 OKC      80    80  2389   5.6   9.9     0.571
##  6     4 Arron… SG       31 SAC      61    45  1580   4.2   9.6     0.44 
##  7     5 Alexi… C        28 NOP      39    15   584   5.5  11       0.5  
##  8     6 Cole … C        28 MIN      62     0   531   3.1   5.8     0.523
##  9     7 LaMar… PF       31 SAS      72    72  2335   7.7  16.2     0.477
## 10     8 Lavoy… PF       27 IND      61     5   871   3.2   6.9     0.458
## # ... with 585 more rows, and 19 more variables: x3p <dbl>, x3pa <dbl>,
## #   x3ppercent <dbl>, x2p <dbl>, x2pa <dbl>, x2ppercent <dbl>, ft <dbl>,
## #   fta <dbl>, ftpercent <dbl>, orb <dbl>, drb <dbl>, trb <dbl>,
## #   ast <dbl>, stl <dbl>, blk <dbl>, tov <dbl>, pf <dbl>, pts <dbl>,
## #   link <chr>

Example - Look at Centers and Power Forwards averaging more than 10 MPG

players <- NBAPerGameStatisticsPer36Min(season = 2017) %>%
  dplyr::filter(pos %in% c("C", "PF")) %>%
  dplyr::top_n(n = 10, pts) %>% 
  dplyr::select(player, link) %>%
  dplyr::distinct()
players
## # A tibble: 8 x 2
##   player             link                     
##   <chr>              <chr>                    
## 1 DeMarcus Cousins   /players/c/couside01.html
## 2 Anthony Davis      /players/d/davisan02.html
## 3 Kevin Durant       /players/d/duranke01.html
## 4 Joel Embiid        /players/e/embiijo01.html
## 5 Enes Kanter        /players/k/kanteen01.html
## 6 Brook Lopez        /players/l/lopezbr01.html
## 7 Boban Marjanovic   /players/m/marjabo01.html
## 8 Karl-Anthony Towns /players/t/townska01.html

Query each player in the list

player_stats <- NBAPlayerPerGameStats(players[1, 2]) %>%
  dplyr::filter(!is.na(age)) %>%
  dplyr::mutate(player = as.character(players[1, 1]))

Append the stats from each player into a df

for(i in 2:dim(players)[1]){
  tmp <- NBAPlayerPerGameStats(players[i, 2]) %>%
    dplyr::filter(!is.na(age)) %>%
    dplyr::mutate(player = as.character(players[i, 1]))
  player_stats <- dplyr::bind_rows(player_stats, tmp)
}

Plot everything

p <- ggplot2::ggplot(data = player_stats,
            aes(x = age, y = efgpercent, group = player))
p + ggplot2::geom_line(alpha = .25) +
  ggplot2::geom_point(alpha = .25) +
  ggplot2::scale_y_continuous("effective field goal %age", limit = c(0, 1),
                     labels = percent) +
  ggplot2::geom_line(data = dplyr::filter(player_stats, player == "Anthony Davis"),
            aes(x = age, y = efgpercent), size = 1, col = "#1f78b4") +
  ggplot2::geom_point(data = dplyr::filter(player_stats, player == "Anthony Davis"),
            aes(x = age, y = efgpercent), size = 1, col = "#1f78b4") +
  ggplot2::geom_line(data = dplyr::filter(player_stats, player == "DeMarcus Cousins"),
            aes(x = age, y = efgpercent), size = 1, col = "#33a02c") +
  ggplot2::geom_point(data = dplyr::filter(player_stats, player == "DeMarcus Cousins"),
             aes(x = age, y = efgpercent), size = 1, col = "#33a02c") +
  ggplot2::theme_bw()

Advanced Statistics

per_100 <- NBAPerGameStatisticsPer100Poss(season = 2018)
utils::head(per_100)
##   rk        player pos age  tm  g gs   mp  fg  fga fgpercent x3p x3pa
## 1  1  Alex Abrines  SG  24 OKC 75  8 1134 5.0 12.7     0.395 3.7  9.7
## 2  2    Quincy Acy  PF  27 BRK 70  8 1359 4.6 13.0     0.356 3.6 10.4
## 3  3  Steven Adams   C  24 OKC 76 76 2487 8.9 14.2     0.629 0.0  0.0
## 4  4   Bam Adebayo   C  20 MIA 69 19 1368 6.4 12.5     0.512 0.0  0.3
## 5  5 Arron Afflalo  SG  32 ORL 53  3  682 4.7 11.6     0.401 1.9  5.0
## 6  6  Cole Aldrich   C  29 MIN 21  0   49 5.1 15.3     0.333 0.0  0.0
##   x3ppercent x2p x2pa x2ppercent  ft fta ftpercent orb  drb  trb ast stl
## 1      0.380 1.4  3.1      0.443 1.7 2.0     0.848 1.1  3.9  5.0 1.2 1.7
## 2      0.349 1.0  2.6      0.384 1.8 2.1     0.817 1.4  7.8  9.2 2.0 1.2
## 3      0.000 8.9 14.2      0.631 3.2 5.7     0.559 7.7  6.0 13.7 1.8 1.8
## 4      0.000 6.4 12.2      0.523 4.7 6.6     0.721 4.3  9.7 14.0 3.7 1.2
## 5      0.386 2.7  6.6      0.413 1.6 1.9     0.846 0.3  4.5  4.7 2.2 0.3
## 6         NA 5.1 15.3      0.333 2.0 6.1     0.333 3.1 12.2 15.3 3.1 2.0
##   blk tov   pf  pts  x ortg drtg                      link
## 1 0.4 1.1  5.4 15.4 NA  116  110 /players/a/abrinal01.html
## 2 1.0 2.1  5.3 14.7 NA   99  110   /players/a/acyqu01.html
## 3 1.6 2.6  4.3 21.1 NA  125  107 /players/a/adamsst01.html
## 4 1.5 2.4  5.1 17.5 NA  116  105 /players/a/adebaba01.html
## 5 0.6 1.5  4.0 12.8 NA   98  115 /players/a/afflaar01.html
## 6 1.0 1.0 11.2 12.2 NA   85  107 /players/a/aldrico01.html

Advanced Statistics

adv_stats <- NBAPerGameAdvStatistics(season = 2018)
utils::head(adv_stats)
##   rk        player pos age  tm  g   mp  per tspercent x3par   ftr
## 1  1  Alex Abrines  SG  24 OKC 75 1134  9.0     0.567 0.759 0.158
## 2  2    Quincy Acy  PF  27 BRK 70 1359  8.2     0.525 0.800 0.164
## 3  3  Steven Adams   C  24 OKC 76 2487 20.6     0.630 0.003 0.402
## 4  4   Bam Adebayo   C  20 MIA 69 1368 15.7     0.570 0.021 0.526
## 5  5 Arron Afflalo  SG  32 ORL 53  682  5.8     0.516 0.432 0.160
## 6  6  Cole Aldrich   C  29 MIN 21   49  6.0     0.340 0.000 0.400
##   orbpercent drbpercent trbpercent astpercent stlpercent blkpercent
## 1        2.5        8.9        5.6        3.4        1.7        0.6
## 2        3.1       17.0       10.0        6.0        1.2        1.6
## 3       16.6       13.9       15.3        5.5        1.8        2.8
## 4        9.7       21.6       15.6       11.0        1.2        2.5
## 5        0.6       10.1        5.3        6.2        0.3        1.1
## 6        7.0       28.6       17.7        8.2        2.0        1.8
##   tovpercent usgpercent  x  ows dws  ws  ws_48 x_2 obpm dbpm  bpm vorp
## 1        7.4       12.7 NA  1.3 1.0 2.2  0.094  NA -0.5 -1.7 -2.2 -0.1
## 2       13.3       14.4 NA -0.1 1.1 1.0  0.036  NA -2.0 -0.2 -2.2 -0.1
## 3       13.2       16.7 NA  6.7 3.0 9.7  0.187  NA  2.2  1.1  3.3  3.3
## 4       13.6       15.9 NA  2.3 1.9 4.2  0.148  NA -1.6  1.8  0.2  0.8
## 5       10.8       12.5 NA -0.1 0.2 0.1  0.009  NA -4.1 -1.8 -5.8 -0.7
## 6        5.4       16.8 NA -0.1 0.1 0.0 -0.013  NA -7.0  0.1 -6.9 -0.1
##                        link
## 1 /players/a/abrinal01.html
## 2   /players/a/acyqu01.html
## 3 /players/a/adamsst01.html
## 4 /players/a/adebaba01.html
## 5 /players/a/afflaar01.html
## 6 /players/a/aldrico01.html

Example

Look at selector gadget for a team’s website, e.g. Denver Nuggets. Suppose you want to find everybody who played for the Nuggets last year, and then their stats. Remember to use Chrome (ugh).

library(rvest)
## Loading required package: xml2
url <- "http://www.basketball-reference.com/teams/DEN/2017.html"
links <- xml2::read_html(url) %>%
    rvest::html_nodes(".center+ .left a") %>%
    rvest::html_attr('href')
links 
##  [1] "/players/a/arthuda01.html" "/players/b/bartowi01.html"
##  [3] "/players/b/beaslma01.html" "/players/c/chandwi01.html"
##  [5] "/players/f/farieke01.html" "/players/g/gallida01.html"
##  [7] "/players/g/geeal01.html"   "/players/h/harriga01.html"
##  [9] "/players/h/hernaju01.html" "/players/h/hibbero01.html"
## [11] "/players/j/jokicni01.html" "/players/m/millemi01.html"
## [13] "/players/m/mudiaem01.html" "/players/m/murraja01.html"
## [15] "/players/n/nelsoja01.html" "/players/n/nurkiju01.html"
## [17] "/players/o/obryajo01.html" "/players/p/plumlma01.html"
## [19] "/players/s/stokeja01.html"

  1. https://www.urbandictionary.com/define.php?term=baller