How to Get Data on Every NBA Player Using R | by Josh Gonzales, M.A. | Towards Data Science

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.
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)
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.
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.
Let’s do a quick example of getting LeBron James’ data using nbastatR’s “players_career” function.
The players_career function requires only one input at minimum:
a player identifier
You can call this function in R using
players_careers(players = c("LeBron James"))
Note: you could have used players_careers(players = “LeBron James”) instead but I keep it as a list in case I want find data on more than one player.
Calling this function will return 8 tables:
dataPlayerCareerTotalsAllStarSeason
dataPlayerCareerTotalsPostSeason
dataPlayerCareerTotalsRegularSeason
dataPlayerSeasonRankingsPostSeason
dataPlayerSeasonRankingsRegularSeason
dataPlayerSeasonTotalsAllStarSeason
dataPlayerSeasonTotalsPostSeason
dataPlayerSeasonTotalsRegularSeason
There’s a lot of data within each of these tibbles. You should explore what each gives you to get a better sense of nbastatR’s capabilities.
Alternatively, you could have invoked LeBron’s player id instead of his name using:
players_careers(player_ids = c(2544))
This will give you the exact same result, but I prefer using the player_ids method because I know that it won’t give me mistakes on other players less famous than LeBron.
You can also add in another input called “mode” and load data onto a variable if you want something more specific.
For example, to get LeBron’s career totals you can use:
lebron_totals <- players_careers(players = c("LeBron James"),                modes = c("Totals"))
You now have a tibble that looks like this:
The nameTable variable tells you exactly what data is being stored in each dataTable variable. If you click on a dataTable variable, it’ll open up another table with more data.
For example, clicking on the dataTable for SeasonTotalsRegularSeason gives you a table that has all of LeBron’s regular season totals in each of the seasons he’s played in the NBA. (it’s two screenshots because I couldn’t fit all the variables on one screen):
The mode input requires one of the following designations:
Totals
PerGame
Per36
So if you’re more interested in a player’s per game or per 36 minute numbers, use one of the other modes instead. You could also call all three modes using:
lebron_career <- players_careers(players = c("LeBron James"),                modes = c("Totals", "PerGame", "Per36"))
This will give you a “table of tables” with all three modes applied.
This is just the tip of the iceberg. There are so many other functions in nbastatR. This was just a basic way of getting player data using R.
There is a lot of data to explore, so have fun with it! If you have a specific question you’d like me answer in a future post, don’t be afraid to let me know in the comments below.