Through this project I have tried to compare LeBron James, Kobe Bryant & Michael Jordan stats per season.
setwd("C:\\Users\\MARK\\Desktop\\Marwin Documents\\Coursera Courses\\24 v 6 v 23")
list.files()
## [1] "Analysis.R" "BallGame.html" "BallGame.md" "BallGame.Rmd"
## [5] "BallGame_files" "kbdata.xlsx" "lbjdata.xlsx" "mjdata.xlsx"
library(xlsx)
## Warning: package 'xlsx' was built under R version 3.6.3
LBJ<-read.xlsx("./lbjdata.xlsx",sheetIndex=1,rowIndex=1:18)
KB<-read.xlsx("./kbdata.xlsx",sheetIndex=1,rowIndex=1:21)
MJ<-read.xlsx("./mjdata.xlsx",sheetIndex=1,rowIndex=1:20)
head(LBJ)
## Season Age Tm Lg Pos G GS MP FG FGA FG. X3P X3PA X3P. X2P X2PA
## 1 2003-04 19 CLE NBA SG 79 79 39.5 7.9 18.9 0.417 0.8 2.7 0.290 7.1 16.1
## 2 2004-05 20 CLE NBA SF 80 80 42.4 9.9 21.1 0.472 1.4 3.9 0.351 8.6 17.2
## 3 2005-06 21 CLE NBA SF 79 79 42.5 11.1 23.1 0.480 1.6 4.8 0.335 9.5 18.3
## 4 2006-07 22 CLE NBA SF 78 78 40.9 9.9 20.8 0.476 1.3 4.0 0.319 8.6 16.8
## 5 2007-08 23 CLE NBA SF 75 74 40.4 10.6 21.9 0.484 1.5 4.8 0.315 9.1 17.1
## 6 2008-09 24 CLE NBA SF 81 81 37.7 9.7 19.9 0.489 1.6 4.7 0.344 8.1 15.2
## X2P. eFG. FT FTA FT. ORB DRB TRB AST STL BLK TOV PF PTS
## 1 0.438 0.438 4.4 5.8 0.754 1.3 4.2 5.5 5.9 1.6 0.7 3.5 1.9 20.9
## 2 0.499 0.504 6.0 8.0 0.750 1.4 6.0 7.4 7.2 2.2 0.7 3.3 1.8 27.2
## 3 0.518 0.515 7.6 10.3 0.738 0.9 6.1 7.0 6.6 1.6 0.8 3.3 2.3 31.4
## 4 0.513 0.507 6.3 9.0 0.698 1.1 5.7 6.7 6.0 1.6 0.7 3.2 2.2 27.3
## 5 0.531 0.518 7.3 10.3 0.712 1.8 6.1 7.9 7.2 1.8 1.1 3.4 2.2 30.0
## 6 0.535 0.530 7.3 9.4 0.780 1.3 6.3 7.6 7.2 1.7 1.1 3.0 1.7 28.4
head(KB)
## Season Age Tm Lg Pos G GS MP FG FGA FG. X3P X3PA X3P. X2P X2PA
## 1 1996-97 18 LAL NBA SF 71 6 15.5 2.5 5.9 0.417 0.7 1.9 0.375 1.8 4.0
## 2 1997-98 19 LAL NBA SF 79 1 26.0 4.9 11.6 0.428 0.9 2.8 0.341 4.0 8.8
## 3 1998-99 20 LAL NBA SG 50 50 37.9 7.2 15.6 0.465 0.5 2.0 0.267 6.7 13.6
## 4 1999-00 21 LAL NBA SG 66 62 38.2 8.4 17.9 0.468 0.7 2.2 0.319 7.7 15.7
## 5 2000-01 22 LAL NBA SG 68 68 40.9 10.3 22.2 0.464 0.9 2.9 0.305 9.4 19.3
## 6 2001-02 23 LAL NBA SG 80 80 38.3 9.4 20.0 0.469 0.4 1.7 0.250 9.0 18.3
## X2P. eFG. FT FTA FT. ORB DRB TRB AST STL BLK TOV PF PTS
## 1 0.437 0.477 1.9 2.3 0.819 0.7 1.2 1.9 1.3 0.7 0.3 1.6 1.4 7.6
## 2 0.456 0.469 4.6 5.8 0.794 1.0 2.1 3.1 2.5 0.9 0.5 2.0 2.3 15.4
## 3 0.494 0.482 4.9 5.8 0.839 1.1 4.2 5.3 3.8 1.4 1.0 3.1 3.1 19.9
## 4 0.489 0.488 5.0 6.1 0.821 1.6 4.7 6.3 4.9 1.6 0.9 2.8 3.3 22.5
## 5 0.489 0.484 7.0 8.2 0.853 1.5 4.3 5.9 5.0 1.7 0.6 3.2 3.3 28.5
## 6 0.489 0.479 6.1 7.4 0.829 1.4 4.1 5.5 5.5 1.5 0.4 2.8 2.9 25.2
head(MJ)
## Season Age Tm Lg Pos G GS MP FG FGA FG. X3P X3PA X3P. X2P X2PA
## 1 1984-85 21 CHI NBA SG 82 82 38.3 10.2 19.8 0.515 0.1 0.6 0.173 10.1 19.2
## 2 1985-86 22 CHI NBA SG 18 7 25.1 8.3 18.2 0.457 0.2 1.0 0.167 8.2 17.2
## 3 1986-87 23 CHI NBA SG 82 82 40.0 13.4 27.8 0.482 0.1 0.8 0.182 13.2 27.0
## 4 1987-88 24 CHI NBA SG 82 82 40.4 13.0 24.4 0.535 0.1 0.6 0.132 13.0 23.7
## 5 1988-89 25 CHI NBA SG 81 81 40.2 11.9 22.2 0.538 0.3 1.2 0.276 11.6 21.0
## 6 1989-90 26 CHI NBA SG 82 82 39.0 12.6 24.0 0.526 1.1 3.0 0.376 11.5 21.0
## X2P. eFG. FT FTA FT. ORB DRB TRB AST STL BLK TOV PF PTS
## 1 0.526 0.518 7.7 9.1 0.845 2.0 4.5 6.5 5.9 2.4 0.8 3.5 3.5 28.2
## 2 0.474 0.462 5.8 6.9 0.840 1.3 2.3 3.6 2.9 2.1 1.2 2.5 2.6 22.7
## 3 0.491 0.484 10.2 11.9 0.857 2.0 3.2 5.2 4.6 2.9 1.5 3.3 2.9 37.1
## 4 0.546 0.537 8.8 10.5 0.841 1.7 3.8 5.5 5.9 3.2 1.6 3.1 3.3 35.0
## 5 0.553 0.546 8.3 9.8 0.850 1.8 6.2 8.0 8.0 2.9 0.8 3.6 3.0 32.5
## 6 0.548 0.550 7.2 8.5 0.848 1.7 5.1 6.9 6.3 2.8 0.7 3.0 2.9 33.6
str(LBJ)
## 'data.frame': 17 obs. of 30 variables:
## $ Season: Factor w/ 17 levels "2003-04","2004-05",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Age : num 19 20 21 22 23 24 25 26 27 28 ...
## $ Tm : Factor w/ 3 levels "CLE","LAL","MIA": 1 1 1 1 1 1 1 3 3 3 ...
## $ Lg : Factor w/ 1 level "NBA": 1 1 1 1 1 1 1 1 1 1 ...
## $ Pos : Factor w/ 4 levels "PF","PG","SF",..: 4 3 3 3 3 3 3 3 3 1 ...
## $ G : num 79 80 79 78 75 81 76 79 62 76 ...
## $ GS : num 79 80 79 78 74 81 76 79 62 76 ...
## $ MP : num 39.5 42.4 42.5 40.9 40.4 37.7 39 38.8 37.5 37.9 ...
## $ FG : num 7.9 9.9 11.1 9.9 10.6 9.7 10.1 9.6 10 10.1 ...
## $ FGA : num 18.9 21.1 23.1 20.8 21.9 19.9 20.1 18.8 18.9 17.8 ...
## $ FG. : num 0.417 0.472 0.48 0.476 0.484 0.489 0.503 0.51 0.531 0.565 ...
## $ X3P : num 0.8 1.4 1.6 1.3 1.5 1.6 1.7 1.2 0.9 1.4 ...
## $ X3PA : num 2.7 3.9 4.8 4 4.8 4.7 5.1 3.5 2.4 3.3 ...
## $ X3P. : num 0.29 0.351 0.335 0.319 0.315 0.344 0.333 0.33 0.362 0.406 ...
## $ X2P : num 7.1 8.6 9.5 8.6 9.1 8.1 8.4 8.4 9.1 8.7 ...
## $ X2PA : num 16.1 17.2 18.3 16.8 17.1 15.2 15 15.3 16.5 14.5 ...
## $ X2P. : num 0.438 0.499 0.518 0.513 0.531 0.535 0.56 0.552 0.556 0.602 ...
## $ eFG. : num 0.438 0.504 0.515 0.507 0.518 0.53 0.545 0.541 0.554 0.603 ...
## $ FT : num 4.4 6 7.6 6.3 7.3 7.3 7.8 6.4 6.2 5.3 ...
## $ FTA : num 5.8 8 10.3 9 10.3 9.4 10.2 8.4 8.1 7 ...
## $ FT. : num 0.754 0.75 0.738 0.698 0.712 0.78 0.767 0.759 0.771 0.753 ...
## $ ORB : num 1.3 1.4 0.9 1.1 1.8 1.3 0.9 1 1.5 1.3 ...
## $ DRB : num 4.2 6 6.1 5.7 6.1 6.3 6.4 6.5 6.4 6.8 ...
## $ TRB : num 5.5 7.4 7 6.7 7.9 7.6 7.3 7.5 7.9 8 ...
## $ AST : num 5.9 7.2 6.6 6 7.2 7.2 8.6 7 6.2 7.3 ...
## $ STL : num 1.6 2.2 1.6 1.6 1.8 1.7 1.6 1.6 1.9 1.7 ...
## $ BLK : num 0.7 0.7 0.8 0.7 1.1 1.1 1 0.6 0.8 0.9 ...
## $ TOV : num 3.5 3.3 3.3 3.2 3.4 3 3.4 3.6 3.4 3 ...
## $ PF : num 1.9 1.8 2.3 2.2 2.2 1.7 1.6 2.1 1.5 1.4 ...
## $ PTS : num 20.9 27.2 31.4 27.3 30 28.4 29.7 26.7 27.1 26.8 ...
str(KB)
## 'data.frame': 20 obs. of 30 variables:
## $ Season: Factor w/ 20 levels "1996-97","1997-98",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Age : num 18 19 20 21 22 23 24 25 26 27 ...
## $ Tm : Factor w/ 1 level "LAL": 1 1 1 1 1 1 1 1 1 1 ...
## $ Lg : Factor w/ 1 level "NBA": 1 1 1 1 1 1 1 1 1 1 ...
## $ Pos : Factor w/ 2 levels "SF","SG": 1 1 2 2 2 2 2 2 2 2 ...
## $ G : num 71 79 50 66 68 80 82 65 66 80 ...
## $ GS : num 6 1 50 62 68 80 82 64 66 80 ...
## $ MP : num 15.5 26 37.9 38.2 40.9 38.3 41.5 37.6 40.7 41 ...
## $ FG : num 2.5 4.9 7.2 8.4 10.3 9.4 10.6 7.9 8.7 12.2 ...
## $ FGA : num 5.9 11.6 15.6 17.9 22.2 20 23.5 18.1 20.1 27.2 ...
## $ FG. : num 0.417 0.428 0.465 0.468 0.464 0.469 0.451 0.438 0.433 0.45 ...
## $ X3P : num 0.7 0.9 0.5 0.7 0.9 0.4 1.5 1.1 2 2.3 ...
## $ X3PA : num 1.9 2.8 2 2.2 2.9 1.7 4 3.3 5.9 6.5 ...
## $ X3P. : num 0.375 0.341 0.267 0.319 0.305 0.25 0.383 0.327 0.339 0.347 ...
## $ X2P : num 1.8 4 6.7 7.7 9.4 9 9.1 6.8 6.7 10 ...
## $ X2PA : num 4 8.8 13.6 15.7 19.3 18.3 19.5 14.8 14.2 20.7 ...
## $ X2P. : num 0.437 0.456 0.494 0.489 0.489 0.489 0.465 0.463 0.472 0.482 ...
## $ eFG. : num 0.477 0.469 0.482 0.488 0.484 0.479 0.483 0.468 0.482 0.491 ...
## $ FT : num 1.9 4.6 4.9 5 7 6.1 7.3 7 8.2 8.7 ...
## $ FTA : num 2.3 5.8 5.8 6.1 8.2 7.4 8.7 8.2 10.1 10.2 ...
## $ FT. : num 0.819 0.794 0.839 0.821 0.853 0.829 0.843 0.852 0.816 0.85 ...
## $ ORB : num 0.7 1 1.1 1.6 1.5 1.4 1.3 1.6 1.4 0.9 ...
## $ DRB : num 1.2 2.1 4.2 4.7 4.3 4.1 5.6 3.9 4.5 4.4 ...
## $ TRB : num 1.9 3.1 5.3 6.3 5.9 5.5 6.9 5.5 5.9 5.3 ...
## $ AST : num 1.3 2.5 3.8 4.9 5 5.5 5.9 5.1 6 4.5 ...
## $ STL : num 0.7 0.9 1.4 1.6 1.7 1.5 2.2 1.7 1.3 1.8 ...
## $ BLK : num 0.3 0.5 1 0.9 0.6 0.4 0.8 0.4 0.8 0.4 ...
## $ TOV : num 1.6 2 3.1 2.8 3.2 2.8 3.5 2.6 4.1 3.1 ...
## $ PF : num 1.4 2.3 3.1 3.3 3.3 2.9 2.7 2.7 2.6 2.9 ...
## $ PTS : num 7.6 15.4 19.9 22.5 28.5 25.2 30 24 27.6 35.4 ...
str(MJ)
## 'data.frame': 19 obs. of 30 variables:
## $ Season: Factor w/ 19 levels "1984-85","1985-86",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Age : num 21 22 23 24 25 26 27 28 29 30 ...
## $ Tm : Factor w/ 4 levels "CHI","Did Not Play (Minor League Baseballâ\200”Birmingham Barons)",..: 1 1 1 1 1 1 1 1 1 2 ...
## $ Lg : Factor w/ 1 level "NBA": 1 1 1 1 1 1 1 1 1 NA ...
## $ Pos : Factor w/ 2 levels "SF","SG": 2 2 2 2 2 2 2 2 2 NA ...
## $ G : num 82 18 82 82 81 82 82 80 78 NA ...
## $ GS : num 82 7 82 82 81 82 82 80 78 NA ...
## $ MP : num 38.3 25.1 40 40.4 40.2 39 37 38.8 39.3 NA ...
## $ FG : num 10.2 8.3 13.4 13 11.9 12.6 12.1 11.8 12.7 NA ...
## $ FGA : num 19.8 18.2 27.8 24.4 22.2 24 22.4 22.7 25.7 NA ...
## $ FG. : num 0.515 0.457 0.482 0.535 0.538 0.526 0.539 0.519 0.495 NA ...
## $ X3P : num 0.1 0.2 0.1 0.1 0.3 1.1 0.4 0.3 1 NA ...
## $ X3PA : num 0.6 1 0.8 0.6 1.2 3 1.1 1.3 2.9 NA ...
## $ X3P. : num 0.173 0.167 0.182 0.132 0.276 0.376 0.312 0.27 0.352 NA ...
## $ X2P : num 10.1 8.2 13.2 13 11.6 11.5 11.7 11.5 11.7 NA ...
## $ X2PA : num 19.2 17.2 27 23.7 21 21 21.3 21.5 22.7 NA ...
## $ X2P. : num 0.526 0.474 0.491 0.546 0.553 0.548 0.551 0.533 0.514 NA ...
## $ eFG. : num 0.518 0.462 0.484 0.537 0.546 0.55 0.547 0.526 0.515 NA ...
## $ FT : num 7.7 5.8 10.2 8.8 8.3 7.2 7 6.1 6.1 NA ...
## $ FTA : num 9.1 6.9 11.9 10.5 9.8 8.5 8.2 7.4 7.3 NA ...
## $ FT. : num 0.845 0.84 0.857 0.841 0.85 0.848 0.851 0.832 0.837 NA ...
## $ ORB : num 2 1.3 2 1.7 1.8 1.7 1.4 1.1 1.7 NA ...
## $ DRB : num 4.5 2.3 3.2 3.8 6.2 5.1 4.6 5.3 5 NA ...
## $ TRB : num 6.5 3.6 5.2 5.5 8 6.9 6 6.4 6.7 NA ...
## $ AST : num 5.9 2.9 4.6 5.9 8 6.3 5.5 6.1 5.5 NA ...
## $ STL : num 2.4 2.1 2.9 3.2 2.9 2.8 2.7 2.3 2.8 NA ...
## $ BLK : num 0.8 1.2 1.5 1.6 0.8 0.7 1 0.9 0.8 NA ...
## $ TOV : num 3.5 2.5 3.3 3.1 3.6 3 2.5 2.5 2.7 NA ...
## $ PF : num 3.5 2.6 2.9 3.3 3 2.9 2.8 2.5 2.4 NA ...
## $ PTS : num 28.2 22.7 37.1 35 32.5 33.6 31.5 30.1 32.6 NA ...
In this segment I have selected the following stats only: 1. Total Rebounds(TRB) 2. Assists(AST) 3. Steals(STL) 4. Blocks(BLK) 5. Points(PTS) 6. Field Goals% (FG%) 7. 3Field Goal% (FG3%) 8. Effective Field Goal% (eFG%) 9. Free Throws% (FT%).
LBJdf<-data.frame(LBJ$TRB,LBJ$AST,LBJ$STL,LBJ$BLK,LBJ$PTS,LBJ$FG.,LBJ$X3P.,LBJ$eFG.,LBJ$FT.)
colnames(LBJdf)<-c("TRB","AST","STL","BLK","PTS","FG%","FG3%","eFG%","FT%")
head(LBJdf)
## TRB AST STL BLK PTS FG% FG3% eFG% FT%
## 1 5.5 5.9 1.6 0.7 20.9 0.417 0.290 0.438 0.754
## 2 7.4 7.2 2.2 0.7 27.2 0.472 0.351 0.504 0.750
## 3 7.0 6.6 1.6 0.8 31.4 0.480 0.335 0.515 0.738
## 4 6.7 6.0 1.6 0.7 27.3 0.476 0.319 0.507 0.698
## 5 7.9 7.2 1.8 1.1 30.0 0.484 0.315 0.518 0.712
## 6 7.6 7.2 1.7 1.1 28.4 0.489 0.344 0.530 0.780
KBdf<-data.frame(KB$TRB,KB$AST,KB$STL,KB$BLK,KB$PTS,KB$FG.,KB$X3P.,KB$eFG.,KB$FT.)
colnames(KBdf)<-c("TRB","AST","STL","BLK","PTS","FG%","FG3%","eFG%","FT%")
head(KBdf)
## TRB AST STL BLK PTS FG% FG3% eFG% FT%
## 1 1.9 1.3 0.7 0.3 7.6 0.417 0.375 0.477 0.819
## 2 3.1 2.5 0.9 0.5 15.4 0.428 0.341 0.469 0.794
## 3 5.3 3.8 1.4 1.0 19.9 0.465 0.267 0.482 0.839
## 4 6.3 4.9 1.6 0.9 22.5 0.468 0.319 0.488 0.821
## 5 5.9 5.0 1.7 0.6 28.5 0.464 0.305 0.484 0.853
## 6 5.5 5.5 1.5 0.4 25.2 0.469 0.250 0.479 0.829
MJdf<-data.frame(MJ$TRB,MJ$AST,MJ$STL,MJ$BLK,MJ$PTS,MJ$FG.,MJ$X3P.,MJ$eFG.,MJ$FT.)
colnames(MJdf)<-c("TRB","AST","STL","BLK","PTS","FG%","FG3%","eFG%","FT%")
head(MJdf)
## TRB AST STL BLK PTS FG% FG3% eFG% FT%
## 1 6.5 5.9 2.4 0.8 28.2 0.515 0.173 0.518 0.845
## 2 3.6 2.9 2.1 1.2 22.7 0.457 0.167 0.462 0.840
## 3 5.2 4.6 2.9 1.5 37.1 0.482 0.182 0.484 0.857
## 4 5.5 5.9 3.2 1.6 35.0 0.535 0.132 0.537 0.841
## 5 8.0 8.0 2.9 0.8 32.5 0.538 0.276 0.546 0.850
## 6 6.9 6.3 2.8 0.7 33.6 0.526 0.376 0.550 0.848
This function calculates average stats per game in a season for the three players.
stats<-function(x,y,z){
nc<-ncol(x)
LBJ<-numeric(nc)
KB<-numeric(nc)
MJ<-numeric(nc)
for(i in 1:nc){
LBJ[i]<-mean(x[,i],na.rm=TRUE)
KB[i]<-mean(y[,i],na.rm=TRUE)
MJ[i]<-mean(z[,i],na.rm=TRUE)
}
df<-data.frame(LBJ,KB,MJ)
rownames(df)=c("Avg. TRB","Avg. AST","Avg. STL","Avg. BLK","Avg. PTS","Avg. FG%","Avg FG3%","Avg. eFG%","Avg. FT%")
print("Avg. Stats for a game in a Season")
df
}
stats(LBJdf,KBdf,MJdf)
## [1] "Avg. Stats for a game in a Season"
## LBJ KB MJ
## Avg. TRB 7.4529412 5.20000 6.1200000
## Avg. AST 7.4352941 4.76000 5.1400000
## Avg. STL 1.5882353 1.41000 2.2933333
## Avg. BLK 0.7411765 0.46000 0.8333333
## Avg. PTS 27.0705882 24.20000 29.4533333
## Avg. FG% 0.5058824 0.44140 0.4882667
## Avg FG3% 0.3438235 0.31770 0.2839333
## Avg. eFG% 0.5441176 0.47620 0.5000000
## Avg. FT% 0.7317647 0.83495 0.8309333
This function plots stats for their entire playing season.
By default it will print Total PTS per game for all the three players.
For plotting any stat pass the number representing that stat in statplot.
colnames(LBJdf)
## [1] "TRB" "AST" "STL" "BLK" "PTS" "FG%" "FG3%" "eFG%" "FT%"
statplot<-function(s=5)
{
stat<-colnames(LBJdf)[s]
names<-paste("Total",stat,"Per Game")
rng<-range(LBJdf[,s],KBdf[,s],MJdf[,s],na.rm=TRUE)
plot(1:length(LBJdf[,s]),LBJdf[,s],type="l",lwd=3,col="black",ylim=rng,ylab=names,xlab="Season")
lines(1:length(KBdf[,s]),KBdf[,s],type="l",lwd=3,col="purple")
lines(1:length(MJdf[,s]),MJdf[,s],type="l",lwd=3,col="red")
title(main=paste(stat,"Plot"))
legend("bottomright",pch=19,col=c("black","purple","red","yellow"),legend=c("LBJ","KB","MJ","Break-Stats NA"),cex=0.5)
abline(v=c(1:22),lty=3)
}
statplot(1)
statplot(2)
statplot(3)
statplot(4)
statplot(5)
statplot(6)
statplot(7)
statplot(8)
statplot(9)
You can download the data for the players by clicking on their names. The downloaded files will be in .xlsx format which you can convert to .csv format and then use it for analysis.