0

I have data.frame TeamFourFactorsRAPM consisting of 44 columns, I want to subset data frame base on two columns column 41 teamName (consist of team names for all players in the NBA) column 44 mp (consist of how many minutes a player played throughout season)

I want to get the 8 players with the highest minutes played, for every team

'data.frame':   539 obs. of  44 variables:
     $ playerId         : int  101108 1628415 101150 1627783 1627846 1629620 1626153 1629641 1628021 1628035 ...
     $ playerName       : chr  "Chris Paul" "Dillon Brooks" "Lou Williams" "Pascal Siakam" ...
     $ LA_RAPM          : num  1.37 1.33 -0.82 1.91 -0.45 -0.48 0.65 -0.7 0.96 -0.08 ...
     $ LA_RAPM_Rank     : int  37 42 463 20 386 400 104 439 73 268 ...
     $ LA_RAPM__Def     : num  0.58 0.84 -0.69 0.47 0.1 -0.33 0.44 0.11 0.4 0.05 ...
     $ LA_RAPM__Def_Rank: int  76 39 486 100 198 401 102 197 112 237 ...
     $ LA_RAPM__Off     : num  0.79 0.49 -0.14 1.44 -0.55 -0.15 0.2 -0.81 0.56 -0.13 ...
     $ LA_RAPM__Off_Rank: int  63 105 322 15 443 333 166 489 94 318 ...
     $ RA_EFG           : num  0.42 0.88 -0.57 0.48 0.09 -0.22 -1.08 0.05 -0.03 -0.02 ...
     $ RA_EFG_Rank      : int  117 37 460 99 207 357 518 229 259 258 ...
     $ RA_EFG__Def      : num  0.08 0.36 -0.17 -0.04 0.13 -0.09 -0.69 0.25 0.35 0.19 ...
     $ RA_EFG__Def_Rank : int  208 84 387 297 169 346 522 110 88 132 ...
     $ RA_EFG__Off      : num  0.33 0.52 -0.4 0.52 -0.05 -0.13 -0.4 -0.2 -0.37 -0.21 ...
     $ RA_EFG__Off_Rank : int  109 70 449 71 264 329 448 366 443 372 ...
     $ RA_FTR           : num  -0.12 -0.24 0.55 0.26 -0.18 0.05 1.18 -0.42 0.39 -0.19 ...
     $ RA_FTR_Rank      : int  315 356 90 161 337 247 19 414 124 340 ...
     $ RA_FTR__Def      : num  0.53 -0.2 0.01 0.34 0.08 -0.04 0.69 -0.08 -0.09 -0.75 ...
     $ RA_FTR__Def_Rank : int  61 373 241 102 201 276 41 302 315 522 ...
     $ RA_FTR__Off      : num  -0.64 -0.04 0.53 -0.08 -0.26 0.09 0.48 -0.34 0.47 0.56 ...
     $ RA_FTR__Off_Rank : int  504 289 53 305 397 218 65 430 68 49 ...
     $ RA_ORBD          : num  -0.02 -0.3 -1.06 0.01 -0.63 -0.7 -1.59 -0.13 -0.67 0.68 ...
     $ RA_ORBD_Rank     : int  269 357 485 253 437 446 522 300 441 103 ...
     $ RA_ORBD__Def     : num  0.83 0.06 -0.82 -0.25 0.38 -0.78 -1.05 0.08 -0.73 0.01 ...
     $ RA_ORBD__Def_Rank: int  40 236 506 373 123 502 524 227 496 264 ...
     $ RA_ORBD__Off     : num  -0.85 -0.35 -0.23 0.26 -1.01 0.08 -0.54 -0.2 0.06 0.69 ...
     $ RA_ORBD__Off_Rank: int  496 392 355 169 511 220 437 341 227 65 ...
     $ RA_TOV           : num  0.93 0.41 0.13 0.93 -0.24 -0.39 1.84 -0.51 0.1 0.51 ...
     $ RA_TOV_Rank      : int  11 82 191 12 412 457 1 482 211 60 ...
     $ RA_TOV__Def      : num  0.5 0.51 -0.27 0.4 0.09 -0.16 1.12 -0.28 0.17 0.36 ...
     $ RA_TOV__Def_Rank : int  36 33 452 55 193 394 2 456 139 66 ...
     $ RA_TOV__Off      : num  0.43 -0.1 0.4 0.52 -0.32 -0.24 0.72 -0.23 -0.08 0.15 ...
     $ RA_TOV__Off_Rank : int  51 358 62 26 466 438 10 433 336 154 ...
     $ RAPM             : num  1.67 1.84 -0.43 1.68 -0.02 -0.76 -0.02 -0.48 -0.08 0.21 ...
     $ RAPM_Rank        : int  44 34 356 43 248 421 246 368 274 197 ...
     $ RAPM__Def        : num  0.92 1 -0.64 0.22 0.5 -0.51 0.03 0.2 0.44 0.43 ...
     $ RAPM__Def_Rank   : int  53 41 457 177 112 440 248 189 121 122 ...
     $ RAPM__Off        : num  0.75 0.84 0.21 1.46 -0.53 -0.25 -0.04 -0.68 -0.53 -0.23 ...
     $ RAPM__Off_Rank   : int  94 81 179 38 406 342 250 440 407 331 ...
     $ season           : chr  "2020-21" "2020-21" "2020-21" "2020-21" ...
     $ teamId           : int  1610612756 1610612763 1610612746 1610612761 1610612756 1610612760 1610612765 1610612738 1610612745 1610612747 ...
     $ teamName         : chr  "PHX" "MEM" "LAC" "TOR" ...
     $ primaryKey       : chr  "101108_2020-21" "1628415_2020-21" "101150_2020-21" "1627783_2020-21" ...
     $ playerRole       : chr  "Ball Handler, Primary Playmaker" "Wing, Shooter" "Ball Handler, Primary Playmaker" "Wing, Playmaker" ...
     $ mp               : num  2199 1997 2846 2006 355 ...
    
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
Drew
  • 5
  • 4

1 Answers1

0

If every player has only 1 row this should work -

result <- TeamFourFactorsRAPM %>% 
             group_by(teamName) %>% 
             slice_max(mp, n = 8) %>% ungroup
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213