Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. lapply(total2[c(2,4,6:53),drop=FALSE],as.numeric)
  2.  
  3. library(XML)
  4. library(ggplot2)
  5.  
  6. # Grabs ADVANCED link and assigns df to ADVANCED table
  7. advanced = "http://www.basketball-
  8. reference.com/leagues/NBA_2017_advanced.html"
  9. advanced.table = readHTMLTable(advanced, header=T, which=1,stringsAsFactors=F)
  10.  
  11. # Grabs REGULAR link and assigns df to REGULAR table
  12. reg = "http://www.basketball-reference.com/leagues/NBA_2017_per_game.html"
  13. reg.table = readHTMLTable(reg,header=T,which = 1,stringsAsFactors=F)
  14.  
  15. # Grab CONTRACTS link and assigns df to CONTRACTS table
  16. contracts = 'http://www.basketball-reference.com/contracts/players.html'
  17. contracts.table = readHTMLTable(contracts,header = T,which=1,stringsAsFactors=F)
  18.  
  19. # Puts them into tables
  20. df <- advanced.table
  21. rdf <- reg.table
  22. cdf <- contracts.table
  23.  
  24. #Changes column name MP to MPG so no confusion later on
  25. colnames(rdf)[8] <- "MPG"
  26.  
  27. # Drops rows with column labels
  28. df <- df[!grepl('Player',df$Player),]
  29. rdf <- rdf[!grepl('Player',rdf$Player),]
  30. cdf <- cdf[!grepl('Player',cdf$Player),]
  31. cdf <- cdf[!grepl('Salary',cdf$Player),]
  32.  
  33. # Creates list of duplicate col names and drops them from rdf and cdf
  34. drops <- c("Pos","Age","Tm","G","Rk")
  35. rdf <- rdf[ , !(names(rdf) %in% drops)]
  36. cdf <- cdf[ , !(names(cdf) %in% drops)]
  37.  
  38. # Merges df and rdf. Then merge total with cdf to create total2
  39. total <- merge(df, rdf,by=c("Player"))
  40. total2 <- merge(total,cdf,by=c("Player"))
  41.  
  42. # Converts selected columns from character to numeric
  43. total[c(2,4,6:53)] <- lapply(total[c(2,4,6:53),drop=FALSE],as.numeric)
  44. total2[c(2,4,6:53)] <- lapply(total2[c(2,4,6:53),drop=FALSE],as.numeric)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement