Skip to content

Instantly share code, notes, and snippets.

@benjaminrobinson
Last active December 9, 2016 07:57
Show Gist options
  • Save benjaminrobinson/48491d42629831f5d1ed787bd5d857f4 to your computer and use it in GitHub Desktop.
Save benjaminrobinson/48491d42629831f5d1ed787bd5d857f4 to your computer and use it in GitHub Desktop.
begin <- Sys.time()
library(jsonlite)
library(dplyr)
options(stringsAsFactors = FALSE)
coach <- data.frame()
for(a in 1:2989){
tmp <- jsonlite::fromJSON(paste0("https://sports.usatoday.com/ajaxservice/ncaa/salaries__coach__",a))
if(is.null(nrow(tmp$rows))){
salary <- data.frame()
coach <- rbind(coach,salary)
} else {
salary <- flatten(cbind(a,tmp$profile$coach_name,ifelse(!is.null(substr(tmp$profile$head_shot,22,26)),substr(tmp$profile$head_shot,22,26),NA),tmp$rows))
rownames(salary) <- 1:nrow(salary)+nrow(coach)
if(ncol(salary)!=16){
print(a)
print(salary$`tmp$profile$coach_name`[1])
salary <- as.data.frame(as.matrix(salary))
coach <- bind_rows(coach,salary)
} else {
print(a)
print(salary$`tmp$profile$coach_name`[1])
coach <- rbind(coach,salary)
}
}
}
strip_num <- function(x) {
x <- as.numeric(gsub("[^[:alnum:][:blank:]+?&/\\-]","",x))
return(x)
}
coach <- coach %>%
select(1:4,6:7,9,11,13,15,17) %>%
rename(coach_id=a,
coach_name=`tmp$profile$coach_name`,
sport=`ifelse(!is.null(substr(tmp$profile$head_shot, 22, 26)), substr(tmp$profile$head_shot, `) %>%
mutate(total_pay.value=strip_num(total_pay.value),
school_pay.value=strip_num(school_pay.value),
other_pay.value=strip_num(other_pay.value),
max_bonus.value=strip_num(max_bonus.value),
last_year_bonus.value=strip_num(last_year_bonus.value),
year.value=strip_num(year.value),
coach_id=strip_num(coach_id),
sport=ifelse(position.value %in% c('director','strength'),'athletics',sport))
write.csv(coach,"C:/Users/Benjamin/Desktop/Projects/coaching_salaries.csv")
end <- Sys.time()
print(end-begin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment