2

I have a fantasy football lineup optimiser using Rgplk. It uses a for loop to generate multiple optimal lineups, the number of which is input by the user. The code is as follows:

Lineups <- list()
for(i in 1:Lineup_no)
{
matrix <- rbind(as.numeric(Data$Position == "QB"), # num QB
       as.numeric(Data$Position == "RB"), # num RB
       as.numeric(Data$Position == "RB"), # num RB
       as.numeric(Data$Position == "WR"), # num WR
       as.numeric(Data$Position == "WR"), # num WR
       as.numeric(Data$Position == "TE"), # num TE
       as.numeric(Data$Position == "TE"), # num TE
       as.numeric(Data$Position %in% c("RB", "WR", "TE")),  # Num RB/WR/TE
       as.numeric(Data$Position == "DEF"),# num DEF
       Data$Salary,Data$fpts,Data$Player.Name==Inclusions)
direction <- c("==",">=","<=",">=","<=",">=","<=","==","==","<=","<","==")
opt_var<-Score-0.01         
rhs<-c(1,2,4,2,4,1,2,7,1,100000,opt_var,length(Inclusions))
sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
                  types = var.types, max = TRUE)
Lineup<-data.frame(Data[sol$solution==1,])
Lineup<-Lineup[order(Lineup$Position),]
Salary<-sum(Lineup$Salary)
Score<-sum(Lineup$fpts)
print(Lineup)
print(Salary)
print(Score)
Lineups[i]<-Lineup
}

For example, if I want to run 3 optimal lineups, then I get the following output:

     Position       Player.Name     Team Opponent Salary  PPG    fpts positionRank   upper   lower Off_Snaps Pct_Off
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
335        RB    Danny Woodhead Chargers  Jaguars  10650 23.0 15.4280            3 19.7669   10.14        50     68%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
1034       TE      Travis Kelce   Chiefs   Texans   9000 13.4 11.7962            7 13.6508   10.23        62     87%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%
[1] 99600
[1] 142.09
     Position       Player.Name     Team Opponent Salary  PPG    fpts positionRank   upper   lower Off_Snaps Pct_Off
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1091       RB   Latavius Murray  Raiders  Falcons   9950 14.2 14.4686            8 17.8615   11.86        45     62%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
585        TE     Gary Barnidge   Browns   Ravens   9600  0.0 12.5613            4  14.924   10.21        51     98%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%
[1] 99500
[1] 141.8957
     Position       Player.Name     Team Opponent Salary  PPG    fpts positionRank   upper   lower Off_Snaps Pct_Off
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
1132       RB     C.J. Anderson  Broncos    Colts  10200 29.9 14.4519            9 15.8198 10.8164        48     83%
585        TE     Gary Barnidge   Browns   Ravens   9600  0.0 12.5613            4  14.924   10.21        51     98%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%
[1] 99750
[1] 141.879

At the completion of the for loop I want to rbind each of the lineups so that it looks something like this:

     Position       Player.Name     Team Opponent Salary  PPG    fpts positionRank   upper   lower Off_Snaps Pct_Off
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
335        RB    Danny Woodhead Chargers  Jaguars  10650 23.0 15.4280            3 19.7669   10.14        50     68%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
1034       TE      Travis Kelce   Chiefs   Texans   9000 13.4 11.7962            7 13.6508   10.23        62     87%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1091       RB   Latavius Murray  Raiders  Falcons   9950 14.2 14.4686            8 17.8615   11.86        45     62%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
585        TE     Gary Barnidge   Browns   Ravens   9600  0.0 12.5613            4  14.924   10.21        51     98%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%
694       DEF           Vikings  Vikings  Packers   5850 21.0 11.4202            6 12.7234    5.95        NA    <NA>
2191       QB   Jimmy Garoppolo Patriots Dolphins  12950 15.1 16.2307           12 17.1451    14.3        71    100%
604        RB DeAngelo Williams Steelers  Bengals   9600 35.1 16.7202            2 20.2447   12.68        56     82%
613        RB   Rashad Jennings   Giants   Saints   8700  8.8 13.7357           15 16.1405   11.08        32     57%
1105       RB      Spencer Ware   Chiefs   Texans   9650 32.9 14.1579           12 16.2301   11.69        34     48%
1132       RB     C.J. Anderson  Broncos    Colts  10200 29.9 14.4519            9 15.8198 10.8164        48     83%
585        TE     Gary Barnidge   Browns   Ravens   9600  0.0 12.5613            4  14.924   10.21        51     98%
452        WR     Antonio Brown Steelers  Bengals  18900 32.6 23.7736            1 25.0086 22.7394        66     97%
1014       WR   DeAndre Hopkins   Texans   Chiefs  14300 16.4 18.8275            4 20.9531 17.3538        73     97%

However, only the Position column seems to be getting added to the list Lineups and not each complete dataframe.

[[1]]
[1] "DEF" "QB"  "RB"  "RB"  "RB"  "RB"  "TE"  "WR"  "WR" 

[[2]]
[1] "DEF" "QB"  "RB"  "RB"  "RB"  "RB"  "TE"  "WR"  "WR" 

[[3]]
[1] "DEF" "QB"  "RB"  "RB"  "RB"  "RB"  "TE"  "WR"  "WR" 

How do I update my code so that each complete lineup dataframe is included in the list Lineups and then subsequently bind together the lineups to create a dataframe like shown above?

Morts81
  • 419
  • 3
  • 13

1 Answers1

1

We can change the [ to [[ to assign the values of 'Lineup' to the list 'Lineups' in the for loop. Also, it may be better to create the 'Lineups' with the required length

Lineups <- vector("list", length(lineup_no))

for(i in seq_along(Lineup_no)){
...
...
  Lineups[[i]] <-Lineup
}
akrun
  • 874,273
  • 37
  • 540
  • 662