Rglpk-梦幻足球阵容优化器-For循环输出的绑定 [英] Rglpk - Fantasy Football Lineup Optimiser - Rbind of For Loop Output
问题描述
我有一个使用Rgplk的梦幻足球阵容优化器.它使用for循环生成多个最佳阵容,其数量由用户输入.代码如下:
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
}
例如,如果我要运行3个最佳阵容,则会得到以下输出:
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
在for循环结束时,我想重新排列每个阵容,使其看起来像这样:
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?
推荐答案
我们可以将[
更改为[[
,以便将for
循环中的'Lineup'的值分配给list
'Lineups' .另外,最好以所需的长度创建阵容"
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
}
这篇关于Rglpk-梦幻足球阵容优化器-For循环输出的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!