In this article you'll learn how to:
- install and load nbastatR
- get every NBA player loaded into R
- get a player's career statistics

The NBA season is on hold. Which, you could look at as a negative and just watch “The Last Dance” on repeat.Or, you could look at this as an opportunity to finally learn how to do some data science on the NBA and really learn the history of the league and the players.

Install and load

There are a few R packages that that let you grab NBA data. I use the one called nbastatR because it’s the one with the best implementation in my opinion. To install the package, use the following command:

devtools::install_github("abresler/nbastatR")

Then all you need to do to load the package is use:

library(nbastatR)

Base functionality: get every NBA player

There are a lot of functions within nbastatR, but let’s first get a matrix of every NBA player using:

assign_nba_players()

This will load a tibble called “df_dict_nba_players” with every NBA player. Ever.

Image for post

Each observation is a player, and there are 17 variables for each player. The ones that are immediately relevant are:

  • namePlayer: the name of the player (duh)idPlayer: the id number of the player (note: this is actually super important)**isActive: **indicates whether a player is actively playing or not

You can explore the other variables at your leisure. The important part is that you now know the id number of every player. While many of the functions in nbastatR can still work if you use player names, some NBA players have similar names, and typos are easily made. If you use a player’s id you will never have these problems.

#data-science #nba #basketball #r #sports #data analysis

How to Get Data on Every NBA Player Using R
13.30 GEEK