Skip to content

Instantly share code, notes, and snippets.

@hjgoode3
Created November 5, 2021 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjgoode3/29e1425aa0587820d86c0b623dc0e4ec to your computer and use it in GitHub Desktop.
Save hjgoode3/29e1425aa0587820d86c0b623dc0e4ec to your computer and use it in GitHub Desktop.
Random Walk script for selecting fantasy football lineup
max_val_team = data.frame(matrix(rep(0, 45),nrow = 9, ncol = 5)) ## initiate date frame of empty lineup
colnames(max_val_team) <- c('LastName', 'FirstName', 'Position',
'Salary', 'Proj_Points')
random_walk <- function(x, trials) { ## input data frame with player info and number of trials to run
for (i in 1:trials){
qb <- x %>% filter(Pos == "QB") %>% sample_n(1) #select the required number of players at each position
rb <- x %>% filter(Pos == "RB") %>% sample_n(2)
wr <- x %>% filter(Pos == "WR") %>% sample_n(3)
te <- x %>% filter(Pos == "TE") %>% sample_n(1)
dst <- x%>% filter(Pos == "DST") %>% sample_n(1)
flex <- x %>% filter(Pos %in% c("RB", "WR", "TE")) %>% sample_n(1)
team = rbind(qb, rb, wr, te, dst, flex) #combine data frame for full team
team_total = sum(team$Proj_Points) #sum total points for generated team at each iteration
team_salary = sum(team$Salary) #sum total salary for generated team at each iteration
max_total = sum(max_val_team$Proj_Points) # sum total points for the team with highest value (previous iterations)
if (team_total > max_total & team_salary <= 50000) { #check if current iteration is new maximum value team
max_val_team = team
}
}
return(max_val_team)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment