Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.65 KB | None | 0 0
  1. #load in data
  2. nbaPts <- read.csv("Downloads/Seasons_Stats.csv")
  3. nbaPls <- read.csv("Downloads/player_data.csv")
  4. nbaMon <- read.csv("Downloads/NBA_season1718_salary.csv")
  5.  
  6. #get heights and names
  7. nba <- nbaPls[c("name", "height")]
  8. nba$height <- sapply(1:length(nba$height), function(x) as.numeric(strsplit(as.character(nba$height[x]), "-")[[1]])[1]*12 + as.numeric(strsplit(as.character(nba$height[x]), "-")[[1]])[2])
  9.  
  10. #process modern data
  11. nbaMon$height <- sapply(1:length(nbaMon$season17_18), function(x) nba$height[as.character(nba$name) == as.character(nbaMon$Player[x])])
  12. nbaMon <- nbaMon[sapply(1:length(nbaMon$height), function(x) length(nbaMon$height[[x]])) == 1,]
  13. nbaMon$height <- as.numeric(unlist(nbaMon$height))
  14.  
  15. #process historical data
  16. nba$games <- sapply(1:length(nba$name), function(x) sum(nbaPts$G[as.character(nbaPts$Player) == as.character(nba$name[x])]))
  17. nba$pts <- sapply(1:length(nba$name), function(x) sum(nbaPts$PTS[as.character(nbaPts$Player) == as.character(nba$name[x])]))
  18. nba$min <- sapply(1:length(nba$name), function(x) sum(nbaPts$MP[as.character(nbaPts$Player) == as.character(nba$name[x])],na.rm = T))
  19. nba$eff <- sapply(1:length(nba$name), function(x) mean(nbaPts$PER[as.character(nbaPts$Player) == as.character(nba$name[x])],na.rm = T))
  20. nba$PointsPerGame <- nba$pts / nba$games
  21. nba$PointsPerMinute <- nba$pts / nba$min
  22. nba <- nba[nba$games != 0 & nba$min != 0,]
  23.  
  24. #create figure
  25. dev.off()
  26. par(mfrow = c(1,4))
  27. plot(nba$height, nba$PointsPerMinute, col = rgb(0,0,0,0.2), ylim = c(0,1.6), xlab = "Player Height (inches)", ylab = "Points Scored Per Minute Played")
  28. text(paste0("r = ", round(cor(nba$height, nba$PointsPerMinute, use = "com"), 4)), x = par("usr")[2]*0.975, y = par("usr")[4]*0.98)
  29. plot(nba$height, nba$PointsPerGame, col = rgb(0,0,0,0.2), xlab = "Player Height (inches)", ylab = "Points Scored Per Game Played")
  30. text(paste0("r = ", round(cor(nba$height, nba$PointsPerGame, use = "com"), 4)), x = par("usr")[2]*0.975, y = par("usr")[4]*0.98)
  31. mtext("Comparing Player Performance Metrics to Player Height (after the Selection Filter of Entry into the NBA)", outer = T, cex = 1, side = 3, line = -3)
  32. plot(nba$height, nba$eff, col = rgb(0,0,0,0.2), xlab = "Player Height (inches)", ylab = "Mean Player Efficiency Rating (whatever that is)")
  33. text(paste0("r = ", round(cor(nba$height, nba$eff, use = "com"), 4)), x = par("usr")[2]*0.977, y = par("usr")[4]*0.975)
  34. plot(nbaMon$height, nbaMon$season17_18/1E6, col = rgb(0,0,0,0.5), xlab = "Player Height (inches)", ylab = "Player Salary (2017-2018; Millions USD)")
  35. text(paste0("r = ", round(cor(nbaMon$height, nbaMon$season17_18, use = "com"), 4)), x = par("usr")[2]*0.985, y = par("usr")[4]*0.98)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement