-
-
Save hjgoode3/29e1425aa0587820d86c0b623dc0e4ec to your computer and use it in GitHub Desktop.
Random Walk script for selecting fantasy football lineup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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