0

I'm trying to add two columns to an NBA player dataset. The first column will establish what quartile the player's age is among all players within the dataset. The second additional column will establish which quartile an individual player's age resides in among his position (i.e. Point Guard, Small Forward, Center, etc.). I'm able to use the dplyr package to calculate the subset age quartiles based on player position, but I don't know how to join it back to the original dataset or if this is even the correct approach to take.

I've used dplyr to calculate subset age quartiles based on position. Have attempted to use other packages like fuzzyjoin, but don't feel that comfortable working with them.

#Incorporate necessary packages
library(ballr)
library(magrittr)
library(dplyr)
library(tidyverse)


#Establish full player table
players <- NBAPerGameAdvStatistics(season = 2018)

#Calculates Quartiles for Each Position

Pos_quartiles <- players %>% 
  group_by(pos) %>% 
  summarise(age = list(enframe(quantile(age, probs=c(0.25,0.5,0.75,1.0))))) %>% 
  unnest

I expect to have the players dataset with 664 observations and 32 variables, the last two of which have been added on as a result of this procedure. The additional rows will show a player's age quartile based upon all players included, as well as the player's age quartile based upon his position.

MJP
  • 71
  • 1
  • 1
  • 4

1 Answers1

0

We can use base::cut with quantile to get the appropriate quartile

library(dplyr)
players %>% 
  mutate(quar_all=cut(age, breaks=c(0,quantile(age, probs=c(0.25,0.5,0.75,1.0))),labels = FALSE)) %>% 
  group_by(pos) %>% 
  mutate(quar_pos=cut(age, breaks=unique(c(0,quantile(age, probs=c(0.25,0.5,0.75,1.0)))),labels = FALSE))

Please note in quar_pos I used unique as I got the error

Error in cut.default(age, breaks = quantile(age, probs = c(0.25, 0.5, : 'breaks' are not unique

For a similar error unique was proposed by Didzis Elferts here , so as Didzis mentioned expect less number of quartiles for the affected groups.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37