Welcome to the ballr [baw-ler], as in baller^[https://www.urbandictionary.com/define.php?term=baller]. This is the R resource for your basketball-reference.com needs.
library(ballr)
library(magrittr) library(ggplot2) library(janitor) library(scales)
Current standings
standings <- NBAStandingsByDate() # "YEAR-MO-DY" standings
Standings on an arbitrary date
standings <- NBAStandingsByDate("2015-12-31") standings
players <- NBAPerGameStatistics() players
players <- NBAPerGameStatistics(season = 2017)
players %>% dplyr::filter(mp > 20, pos %in% c("SF")) %>% dplyr::select(player, link) %>% dplyr::distinct()
players <- NBAPerGameStatisticsPer36Min(season = 2017)
players
players <- NBAPerGameStatisticsPer36Min(season = 2019) %>% dplyr::filter(pos %in% c("SF", "PF")) %>% dplyr::top_n(n = 10, pts) %>% dplyr::select(player, link) %>% dplyr::distinct()
players
Query each player in the list and append the stats from each player into a df.
for(i in 1:dim(players)[1]){ print(i)# i <- 7 try({ tmp <- NBAPlayerPerGameStats(players[i, 2]) %>% dplyr::filter(!is.na(age)) %>% dplyr::mutate(player = as.character(players[i, 1])) if(is.numeric(tmp$g)){ if(exists("player_stats")){ player_stats <- rbind(player_stats, tmp) } else{ player_stats <- tmp } } }, silent = T) }
Plot everything
p <- ggplot2::ggplot(data = player_stats, aes(x = age, y = efgpercent, group = player, col = 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::theme_bw()
per_100 <- NBAPerGameStatisticsPer100Poss(season = 2018) utils::head(per_100)
adv_stats <- NBAPerGameAdvStatistics(season = 2018) utils::head(adv_stats)
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) 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.