我有一个使用Rgplk的幻想足球阵容优化器。它使用for循环生成多个最佳阵容,其数量由用户输入。代码如下:
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个最佳阵容,那么我得到以下输出:
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循环后,我想对每个阵容进行训练,使其看起来像这样:
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%
但是,只有“位置”列似乎会添加到列表阵容中,而不是每个完整的数据帧。
[[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"
如何更新我的代码,以便每个完整的阵容数据框都包含在列表阵容中,然后将这些阵容绑定在一起以创建如上所示的数据框?
答案 0 :(得分:1)
我们可以将[
更改为[[
,以指定&#39;阵容&#39;的值。到list
&#39;阵容&#39;在for
循环中。此外,最好是创建&#39;阵容&#39;具有所需的长度
Lineups <- vector("list", length(lineup_no))
for(i in seq_along(Lineup_no)){
...
...
Lineups[[i]] <-Lineup
}