这个问题与我试图为我的体育工作优化的一段R代码有关。对于那里的所有体育迷来说,背景是篮球。我的任务是使用以下数据框的比赛和球员ID来估计谁在篮球场上任何时候(5名球员)。数据数据帧对应于一个篮球比赛。因此,我使用的数据集是来自一个篮球比赛的数据,按每个比赛类型细分。篮球队有大约10个独特的球员ID。我有一些可以使用代码的可行数据:
team1 = c(76580, 76584, 76593, 76586, 76583, 76585, 76588, 76596, 76582, 76592, 76589, 76587)
data = structure(list(Tag_ID = c(69, 65, 74, 113, 64, 74, 77, 97, 64,
74, 80, 85, 67, 74, 86, 117, 73, 91, 64, 74, 71, 82, 83, 107,
63, 95, 63, 90, 64, 74, 77, 85, 67, 74, 77, 85, 67, 74, 71, 89,
64, 74, 87, 64, 74, 78, 97, 64, 74, 88, 64, 74, 87, 64, 74, 88,
64, 70, 77, 85, 67, 74, 77, 85, 67, 73, 78, 97, 64, 70, 92, 64,
74, 77, 85, 67, 74, 80, 97, 64, 70, 71, 92, 64, 74, 78, 97, 64,
74, 78, 82, 67, 85, 74, 72, 78, 86, 117, 74, 72, 80, 85, 67,
73, 92, 64, 74, 101, 63, 90, 63, 90, 63, 90, 64, 74, 97, 64,
74, 77, 97, 64, 74, 89, 64, 74, 91, 64, 74, 77, 83, 112, 64,
74, 80, 85, 67, 74, 99, 69, 64, 74, 78, 97, 64, 74, 77, 67, 67,
74, 80, 67, 67, 74, 86, 117, 73, 92, 64, 74, 111, 64, 74, 77,
83, 82, 67, 67, 74, 80, 85, 67, 74, 88, 64, 74, 107, 63, 90,
63, 81, 97, 64, 74, 78, 86, 117, 74, 89, 64, 74, 80, 85, 67,
74, 77, 83, 80, 85, 67, 73, 80, 83, 109, 63, 95, 63, 90, 64,
74, 71, 92, 64, 74, 72, 107, 63, 95, 63, 90, 64, 74, 92, 64,
74, 82, 85, 67, 74, 82, 83, 106, 63, 90, 64, 74, 77, 85, 67,
74, 77, 83, 113, 64, 74, 71, 114, 64, 74, 80, 83, 106, 63, 81,
97, 64, 73, 97, 64, 74, 113, 64, 74, 82, 85, 67, 74, 80, 85,
67, 73, 92, 69, 64, 74, 72, 91, 64, 74, 80, 85, 67, 73, 97, 64,
74, 87, 64, 74, 80, 85, 67, 74, 77, 85, 67, 74, 88, 64, 70, 112,
64, 74, 87, 64, 74, 98, 64, 74, 71, 92, 64, 74, 86, 117, 73,
92, 64, 74, 71, 82, 97, 64, 74, 78, 97, 64, 76, 86, 117, 73,
107, 63, 90, 63, 90, 64, 74, 78, 97, 64, 74, 80, 83, 105, 63,
90, 67, 74, 92, 64, 74, 86, 117, 73, 88, 64, 74, 72, 91, 64,
74, 97, 64, 74, 86, 117, 74, 87, 64, 74, 71, 100, 63, 90, 64,
74, 88, 64, 70, 77, 97, 64, 74, 87, 64, 74, 101, 63, 95, 63,
95, 63, 81, 85, 67, 74, 99, 69, 64, 74, 82, 85, 67, 74, 87, 64,
74, 86, 117, 73, 92, 64, 74, 87, 64, 74, 97, 64, 74, 77, 85,
67, 74, 106, 63, 90, 64, 74, 88, 64, 76, 72, 92, 64, 74, 72,
82, 83, 88, 64, 74, 82, 85, 67, 74, 87, 64, 74, 78, 97, 64, 74,
86, 117, 73, 107, 63, 95, 63, 81, 85, 67, 74, 72, 92, 64, 74,
80, 85, 67, 74, 80, 85, 67, 74, 88, 64, 74, 77, 85, 67, 74, 71,
78, 86, 117, 74, 82, 83, 82, 110, 64, 74, 78, 117, 74, 82, 110,
64, 70, 91, 64, 74, 88, 63, 81, 85, 67, 74, 97, 64, 74, 99),
Tag_ID_Play = c("New Period", "Jumpball", "Halfcourt", "Violation",
"Inbounds", "Halfcourt", "3pt Miss", "OOB", "Inbounds", "Halfcourt",
"Jumper Miss", "DRB", "DRB", "Halfcourt", "Steal", "Steal",
"Fastbreak", "Jumper Make", "Inbounds", "Halfcourt", "Drive Left",
"Layup Miss", "ORB", "Shoot Foul Layup Miss", "FT Attempt",
"Non-Last FT Miss", "FT Attempt", "FT Make", "Inbounds",
"Halfcourt", "3pt Miss", "DRB", "DRB", "Halfcourt", "3pt Miss",
"DRB", "DRB", "Halfcourt", "Drive Left", "Dunk Make", "Inbounds",
"Halfcourt", "3pt Make", "Inbounds", "Halfcourt", "Deflection",
"OOB", "Inbounds", "Halfcourt", "Def Foul", "Inbounds", "Halfcourt",
"3pt Make", "Inbounds", "Halfcourt", "Def Foul", "Inbounds",
"Baseline", "3pt Miss", "DRB", "DRB", "Halfcourt", "3pt Miss",
"DRB", "DRB", "Fastbreak", "Deflection", "OOB", "Inbounds",
"Baseline", "Layup Make", "Inbounds", "Halfcourt", "3pt Miss",
"DRB", "DRB", "Halfcourt", "Jumper Miss", "OOB", "Inbounds",
"Baseline", "Drive Left", "Layup Make", "Inbounds", "Halfcourt",
"Deflection", "OOB", "Inbounds", "Halfcourt", "Deflection",
"Layup Miss", "DRB", "DRB", "Halfcourt", "Drive Right", "Deflection",
"Steal", "Steal", "Halfcourt", "Drive Right", "Jumper Miss",
"DRB", "DRB", "Fastbreak", "Layup Make", "Inbounds", "Halfcourt",
"Shoot Foul 3pt Miss", "FT Attempt", "FT Make", "FT Attempt",
"FT Make", "FT Attempt", "FT Make", "Inbounds", "Halfcourt",
"OOB", "Inbounds", "Halfcourt", "3pt Miss", "OOB", "Inbounds",
"Halfcourt", "Dunk Make", "Inbounds", "Halfcourt", "Jumper Make",
"Inbounds", "Halfcourt", "3pt Miss", "ORB", "Tipin Make",
"Inbounds", "Halfcourt", "Jumper Miss", "DRB", "DRB", "Halfcourt",
"Period Ends Play", "New Period", "Inbounds", "Halfcourt",
"Deflection", "OOB", "Inbounds", "Halfcourt", "3pt Miss",
"DRB", "DRB", "Halfcourt", "Jumper Miss", "DRB", "DRB", "Halfcourt",
"Steal", "Steal", "Fastbreak", "Layup Make", "Inbounds",
"Halfcourt", "Timeout", "Inbounds", "Halfcourt", "3pt Miss",
"ORB", "Layup Miss", "DRB", "DRB", "Halfcourt", "Jumper Miss",
"DRB", "DRB", "Halfcourt", "Def Foul", "Inbounds", "Halfcourt",
"Shoot Foul Layup Miss", "FT Attempt", "FT Make", "FT Attempt",
"Last FT Miss", "OOB", "Inbounds", "Halfcourt", "Deflection",
"Steal", "Steal", "Halfcourt", "Dunk Make", "Inbounds", "Halfcourt",
"Jumper Miss", "DRB", "DRB", "Halfcourt", "3pt Miss", "ORB",
"Jumper Miss", "DRB", "DRB", "Fastbreak", "Jumper Miss",
"ORB", "Shoot Foul Tipin Miss", "FT Attempt", "Non-Last FT Miss",
"FT Attempt", "FT Make", "Inbounds", "Halfcourt", "Drive Left",
"Layup Make", "Inbounds", "Halfcourt", "Drive Right", "Shoot Foul Layup Miss",
"FT Attempt", "Non-Last FT Miss", "FT Attempt", "FT Make",
"Inbounds", "Halfcourt", "Layup Make", "Inbounds", "Halfcourt",
"Layup Miss", "DRB", "DRB", "Halfcourt", "Layup Miss", "ORB",
"Shoot Foul Layup Make", "FT Attempt", "FT Make", "Inbounds",
"Halfcourt", "3pt Miss", "DRB", "DRB", "Halfcourt", "3pt Miss",
"ORB", "Violation", "Inbounds", "Halfcourt", "Drive Left",
"Charge", "Inbounds", "Halfcourt", "Jumper Miss", "ORB",
"Shoot Foul Layup Make", "FT Attempt", "Last FT Miss", "OOB",
"Inbounds", "Fastbreak", "OOB", "Inbounds", "Halfcourt",
"Violation", "Inbounds", "Halfcourt", "Layup Miss", "DRB",
"DRB", "Halfcourt", "Jumper Miss", "DRB", "DRB", "Fastbreak",
"Layup Make", "New Period", "Inbounds", "Halfcourt", "Drive Right",
"Jumper Make", "Inbounds", "Halfcourt", "Jumper Miss", "DRB",
"DRB", "Fastbreak", "OOB", "Inbounds", "Halfcourt", "3pt Make",
"Inbounds", "Halfcourt", "Jumper Miss", "DRB", "DRB", "Halfcourt",
"3pt Miss", "DRB", "DRB", "Halfcourt", "Def Foul", "Inbounds",
"Baseline", "Tipin Make", "Inbounds", "Halfcourt", "3pt Make",
"Inbounds", "Halfcourt", "Other End", "Inbounds", "Halfcourt",
"Drive Left", "Layup Make", "Inbounds", "Halfcourt", "Steal",
"Steal", "Fastbreak", "Layup Make", "Inbounds", "Halfcourt",
"Drive Left", "Layup Miss", "OOB", "Inbounds", "Halfcourt",
"Deflection", "OOB", "Inbounds", "Sideline", "Steal", "Steal",
"Fastbreak", "Shoot Foul Layup Miss", "FT Attempt", "FT Make",
"FT Attempt", "FT Make", "Inbounds", "Halfcourt", "Deflection",
"OOB", "Inbounds", "Halfcourt", "Jumper Miss", "ORB", "Shoot Foul Jumper Miss",
"FT Attempt", "FT Make", "DRB", "Halfcourt", "Layup Make",
"Inbounds", "Halfcourt", "Steal", "Steal", "Fastbreak", "Def Foul",
"Inbounds", "Halfcourt", "Drive Right", "Jumper Make", "Inbounds",
"Halfcourt", "OOB", "Inbounds", "Halfcourt", "Steal", "Steal",
"Halfcourt", "3pt Make", "Inbounds", "Halfcourt", "Drive Left",
"Shoot Foul 3pt Make", "FT Attempt", "FT Make", "Inbounds",
"Halfcourt", "Def Foul", "Inbounds", "Baseline", "3pt Miss",
"OOB", "Inbounds", "Halfcourt", "3pt Make", "Inbounds", "Halfcourt",
"Shoot Foul 3pt Miss", "FT Attempt", "Non-Last FT Miss",
"FT Attempt", "Non-Last FT Miss", "FT Attempt", "Last FT Miss",
"DRB", "DRB", "Halfcourt", "Period Ends Play", "New Period",
"Inbounds", "Halfcourt", "Layup Miss", "DRB", "DRB", "Halfcourt",
"3pt Make", "Inbounds", "Halfcourt", "Steal", "Steal", "Fastbreak",
"Layup Make", "Inbounds", "Halfcourt", "3pt Make", "Inbounds",
"Halfcourt", "OOB", "Inbounds", "Halfcourt", "3pt Miss",
"DRB", "DRB", "Halfcourt", "Shoot Foul Layup Make", "FT Attempt",
"FT Make", "Inbounds", "Halfcourt", "Def Foul", "Inbounds",
"Sideline", "Drive Right", "Layup Make", "Inbounds", "Halfcourt",
"Drive Right", "Layup Miss", "ORB", "Def Foul", "Inbounds",
"Halfcourt", "Layup Miss", "DRB", "DRB", "Halfcourt", "3pt Make",
"Inbounds", "Halfcourt", "Deflection", "OOB", "Inbounds",
"Halfcourt", "Steal", "Steal", "Fastbreak", "Shoot Foul Layup Miss",
"FT Attempt", "Non-Last FT Miss", "FT Attempt", "Last FT Miss",
"DRB", "DRB", "Halfcourt", "Drive Right", "Layup Make", "Inbounds",
"Halfcourt", "Jumper Miss", "DRB", "DRB", "Halfcourt", "Jumper Miss",
"DRB", "DRB", "Halfcourt", "Def Foul", "Inbounds", "Halfcourt",
"3pt Miss", "DRB", "DRB", "Halfcourt", "Drive Left", "Deflection",
"Steal", "Steal", "Halfcourt", "Layup Miss", "ORB", "Layup Miss",
"Tie Up", "Inbounds", "Halfcourt", "Deflection", "Steal",
"Halfcourt", "Layup Miss", "Tie Up", "Inbounds", "Baseline",
"Jumper Make", "Inbounds", "Halfcourt", "Def Foul", "FT Attempt",
"Last FT Miss", "DRB", "DRB", "Halfcourt", "OOB", "Inbounds",
"Halfcourt", "Period Ends Play"), player_id = c(NA, 76580L,
NA, 76584L, NA, NA, 76599L, NA, NA, NA, 76593L, 76607L, 76607L,
NA, 76593L, 76593L, NA, 76580L, NA, NA, 76600L, 76600L, 76607L,
76607L, 76607L, NA, 76607L, 76607L, NA, NA, 76586L, 76599L,
76599L, NA, 76607L, 76593L, 76593L, NA, 76583L, 76580L, NA,
NA, 76599L, NA, NA, 76599L, 76583L, NA, NA, 76586L, NA, NA,
76607L, NA, NA, 76599L, NA, NA, 76583L, 76607L, 76607L, NA,
76607L, 76584L, 76584L, NA, 76605L, NA, NA, NA, 76593L, NA,
NA, 76607L, 76593L, 76593L, NA, 76580L, NA, NA, NA, 76583L,
76583L, NA, NA, 76586L, NA, NA, NA, 76593L, 76605L, 76586L,
76586L, NA, 76586L, 76599L, 76599L, 76599L, NA, 76599L, 76607L,
76584L, 76584L, NA, 76586L, NA, NA, 76599L, 76599L, 76599L,
76599L, 76599L, 76599L, 76599L, NA, NA, 76586L, NA, NA, 76599L,
NA, NA, NA, 76585L, NA, NA, 76607L, NA, NA, 76584L, 76588L,
76588L, NA, NA, 76603L, 76583L, 76583L, NA, NA, NA, NA, NA,
76593L, NA, NA, NA, 76607L, 76580L, 76580L, NA, 76593L, 76607L,
76607L, NA, 76593L, 76593L, NA, 76593L, NA, NA, NA, NA, NA,
76607L, 76605L, 76605L, 76596L, 76596L, NA, 76580L, 76607L,
76607L, NA, 76580L, NA, NA, 76607L, 76607L, 76607L, 76607L,
NA, NA, NA, NA, 76593L, 76593L, 76593L, NA, 76580L, NA, NA,
76607L, 76593L, 76593L, NA, 76593L, 76583L, 76583L, 76605L,
76605L, NA, 76603L, 76607L, 76607L, 76607L, NA, 76607L, 76607L,
NA, NA, 76583L, 76583L, NA, NA, 76603L, 76603L, 76603L, NA,
76603L, 76603L, NA, NA, 76580L, NA, NA, 76607L, 76583L, 76583L,
NA, 76583L, 76580L, 76580L, 76580L, 76580L, NA, NA, 76605L,
76583L, 76583L, NA, 76580L, 76593L, 76583L, NA, NA, 76605L,
76605L, NA, NA, 76583L, 76585L, 76583L, 76583L, NA, NA, NA,
NA, 76607L, NA, NA, NA, NA, NA, 76593L, 76605L, 76605L, NA,
76603L, 76588L, 76588L, NA, 76585L, NA, NA, NA, 76583L, 76580L,
NA, NA, 76599L, 76593L, 76593L, NA, 76593L, NA, NA, 76600L,
NA, NA, 76586L, 76599L, 76599L, NA, 76599L, 76593L, 76593L,
NA, 76599L, NA, NA, 76583L, NA, NA, 76607L, NA, NA, NA, NA,
NA, 76586L, 76586L, NA, NA, 76593L, 76593L, NA, 76584L, NA,
NA, 76603L, 76603L, NA, NA, NA, 76586L, NA, NA, NA, 76586L,
76586L, NA, 76586L, 76586L, 76586L, 76586L, 76586L, NA, NA,
76583L, NA, NA, NA, 76607L, 76606L, 76606L, 76606L, 76606L,
76596L, NA, 76596L, NA, NA, 76593L, 76593L, NA, 76607L, NA,
NA, 76584L, 76586L, NA, NA, 76601L, NA, NA, 76603L, 76603L,
NA, 76601L, NA, NA, 76584L, 76593L, 76593L, 76593L, NA, NA,
76593L, NA, NA, 76599L, NA, NA, NA, 76583L, NA, NA, 76601L,
76601L, NA, 76601L, NA, 76601L, NA, 76596L, 76596L, NA, NA,
NA, NA, NA, 76606L, 76596L, 76596L, NA, 76593L, NA, NA, 76593L,
76593L, NA, 76584L, NA, NA, 76607L, NA, NA, 76584L, NA, NA,
76607L, 76582L, 76582L, NA, 76596L, 76596L, 76596L, NA, NA,
76582L, NA, NA, 76599L, 76599L, NA, NA, 76584L, 76584L, 76596L,
76599L, NA, NA, 76588L, 76603L, 76603L, NA, 76607L, NA, NA,
76602L, NA, NA, NA, 76602L, 76602L, NA, 76603L, 76603L, NA,
76603L, NA, 76585L, 76585L, NA, 76584L, 76585L, NA, NA, 76599L,
76582L, 76582L, NA, 76580L, 76602L, 76602L, NA, 76582L, NA,
NA, 76605L, 76592L, 76592L, NA, 76586L, 76604L, 76604L, 76604L,
NA, 76606L, 76606L, 76606L, 76606L, NA, NA, 76606L, 76606L,
NA, 76606L, 76606L, NA, NA, 76602L, NA, NA, 76602L, 76589L,
NA, 76602L, 76602L, NA, 76601L, NA, NA, NA), player2_id = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 76607L,
76607L, NA, 76583L, NA, NA, NA, NA, NA, 76584L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 76583L, NA,
NA, 76607L, NA, NA, 76583L, NA, NA, NA, 76603L, NA, NA, 76603L,
NA, NA, 76583L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 76593L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 76607L, NA, NA, NA, 76605L, 76593L, NA, NA, NA,
NA, 76585L, 76585L, 76585L, NA, NA, NA, NA, NA, NA, 76584L,
NA, NA, 76583L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 76584L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 76599L, NA, NA, NA, 76580L,
NA, NA, NA, NA, NA, NA, NA, 76603L, 76603L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 76607L,
NA, NA, 76596L, NA, NA, NA, NA, NA, NA, NA, 76606L, 76606L,
76606L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 76593L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 76586L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 76602L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 76584L, NA, NA, NA, NA, 76605L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 76583L, NA, NA, NA, NA, 76583L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 76599L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
76583L, NA, NA, 76584L, NA, NA, 76600L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 76605L, 76605L, NA, 76593L, NA, NA, NA, 76584L,
NA, NA, NA, 76599L, NA, NA, NA, 76600L, 76600L, NA, 76607L,
NA, NA, NA, NA, NA, NA, 76607L, NA, NA, NA, NA, NA, 76588L,
NA, NA, NA, NA, 76593L, NA, NA, 76600L, 76600L, NA, 76586L,
NA, NA, NA, 76584L, NA, NA, NA, NA, NA, 76583L, 76583L, NA,
76607L, NA, NA, NA, 76599L, NA, NA, NA, NA, 76606L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 76586L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 76596L, NA, NA, NA, 76583L, NA,
NA, 76607L, 76607L, NA, 76593L, NA, NA, 76601L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 76599L, NA, NA, NA, NA, 76599L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 76596L, NA, NA, NA, NA, NA,
NA, 76602L, NA, NA, 76584L, NA, NA, NA, 76580L, 76580L, NA,
76580L, NA, NA, NA, NA, NA, NA, NA, NA, 76584L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 76599L, NA, NA, NA, NA, NA, NA,
NA, 76586L, 76586L, 76586L, NA, 76587L, NA, NA, 76592L, NA,
NA, 76592L, 76592L, NA, 76589L, 76589L, NA, NA, NA, NA, NA,
76589L, NA, NA, NA, NA, NA, NA, NA, NA, NA), indicator_newperiod = c(1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Tag_ID",
"Tag_ID_Play", "player_id", "player2_id", "indicator_newperiod"
), class = "data.frame", row.names = c(1L, 512L, 513L, 514L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 47L, 46L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 90L, 89L, 91L, 92L, 93L,
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L,
150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L,
161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L,
172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L,
183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 201L, 198L, 199L, 200L, 202L, 203L, 204L,
205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L,
216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 225L, 226L,
227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L,
238L, 239L, 240L, 241L, 242L, 244L, 243L, 245L, 246L, 247L, 249L,
248L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L,
260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 274L,
270L, 271L, 272L, 273L, 275L, 276L, 277L, 278L, 279L, 280L, 281L,
282L, 284L, 283L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L,
293L, 294L, 295L, 296L, 298L, 297L, 299L, 300L, 301L, 302L, 303L,
304L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L,
315L, 316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 502L, 503L,
504L, 505L, 506L, 507L, 324L, 325L, 326L, 327L, 328L, 329L, 330L,
331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L,
342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L, 351L, 352L,
353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L,
364L, 365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L,
375L, 376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L,
386L, 387L, 388L, 389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L,
397L, 398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L, 407L,
408L, 409L, 410L, 411L, 412L, 413L, 414L, 415L, 416L, 417L, 418L,
419L, 420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L, 429L,
430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L, 439L, 440L,
441L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L, 450L, 451L,
452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L, 461L, 462L,
463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L, 472L, 473L,
474L, 475L, 476L, 477L, 478L, 479L, 480L, 481L, 482L, 483L, 484L,
485L, 486L, 508L, 509L, 510L, 511L, 487L, 488L, 489L, 490L, 491L,
492L, 493L, 494L, 495L, 496L, 497L, 498L, 499L, 500L, 501L))
接下来,下面是我试图填充的on_court数据帧(未填写,仅使用默认的NA初始化)。 on_court数据帧的行数与数据数据帧的行数相同。我有效地猜测谁在场上每场比赛。一旦填写,on_court数据帧的每一行将有5个player_ID。
head(on_court, n = 20)
# P1 P2 P3 P4 P5 Q1 Q2 Q3 Q4 Q5
# 1 NA NA NA NA NA NA NA NA NA NA
# 2 NA NA NA NA NA NA NA NA NA NA
# 3 NA NA NA NA NA NA NA NA NA NA
# 4 NA NA NA NA NA NA NA NA NA NA
# 5 NA NA NA NA NA NA NA NA NA NA
# 6 NA NA NA NA NA NA NA NA NA NA
# 7 NA NA NA NA NA NA NA NA NA NA
# 8 NA NA NA NA NA NA NA NA NA NA
# 9 NA NA NA NA NA NA NA NA NA NA
# 10 NA NA NA NA NA NA NA NA NA NA
# 11 NA NA NA NA NA NA NA NA NA NA
# 12 NA NA NA NA NA NA NA NA NA NA
# 13 NA NA NA NA NA NA NA NA NA NA
# 14 NA NA NA NA NA NA NA NA NA NA
# 15 NA NA NA NA NA NA NA NA NA NA
# 16 NA NA NA NA NA NA NA NA NA NA
# 17 NA NA NA NA NA NA NA NA NA NA
# 18 NA NA NA NA NA NA NA NA NA NA
# 19 NA NA NA NA NA NA NA NA NA NA
# 20 NA NA NA NA NA NA NA NA NA NA
使用两个玩家ID列,对应于每个游戏中涉及的玩家,可以使用以下策略猜测球场上的谁 - 我只是假设最近的5个玩家被标记在事件中的人都在球场上。重要的是要注意数据数据帧按顺序包含篮球比赛的所有比赛,这使得这种方法成为可能。
我的代码使用前后计数器(分别为i和j),总是试图找到最近出现在篮球比赛中的5个玩家ID,并假设这些是球场上的五名球员。这个特定的游戏有514个播放/事件/ nrow(数据)。由于每场比赛最多有2个玩家ID,这意味着我有1028个玩家ID可供使用。我当前的代码迭代了这1028个玩家ID,看起来像这样。此代码应该能够与上面的列表和数据框一起运行:
# 1 - Before the for loop
# 1.a create custom rounding function that rounds 0.5 upwards
round2 = function(x, n) {
posneg = sign(x)
z = abs(x)*10^n
z = z + 0.5
z = trunc(z)
z = z/10^n
z*posneg
}
#1.b create on_court dataframe
on_court = data.frame(matrix(ncol = 10, nrow = nrow(data)))
names(on_court) = c("P1", "P2", "P3", "P4", "P5", "Q1", "Q2", "Q3", "Q4", "Q5")
# 1.c combine the two player ID columns into one column
player_ids_unlisted = as.vector(t(data[, c("player_id", "player2_id")]))
K = 0.525
j = 1
# begin the for loop
for(i in 1:length(player_ids_unlisted)) {
# dividing the counter in half gives the play number
i_2 = round2(i/2, 0)
j_2 = round2(j/2, 0)
# if the playtype is 'new period', bring the back counter j to match the front counter i.
# at the start of a new period of the game, this resets who we think is on the court
if (data$indicator_newperiod[i_2] == 1) {
j = i
j_2 = i_2
}
# unique player_IDs in range of events / counters,
# then remove NA player IDs,
# then filter the list by players only on team 1
uq_players = unique(player_ids_unlisted[j:i])
uq_players = uq_players[!is.na(uq_players)]
uq_players = uq_players[uq_players %in% team1]
# length of unique player_ID list
uq_span = length(uq_players)
# early in the game, if 5 unique player_IDs have yet to appear, then pass
if(uq_span < 5) {
next
}
# if we have exactly 5 players identified, set them as the players on court
else if(uq_span == 5) {
on_court[i_2, 1:5] = uq_players
}
else {
# as the i counter increments, eventually the list of unique players will reach 6 players.
# when that is the case, I increment the back counter j until we get back down to 5 players
# (effectively removing the player who hasn't been tagged in an event for the most time from the list of
# players on court). Then I update whose on court again.
while (uq_span > 5) {
j = j + 1
# again, find IDs, remove NAs, filter by team 1
uq_players = unique(player_ids_unlisted[j:i])
uq_players = uq_players[!is.na(uq_players)]
uq_players = uq_players[uq_players %in% team1]
uq_span = length(uq_players)
}
# this j_2 is the play index that the player we're subbing out last was tagged in an event
# mid_idx is the index between when the j_2 player_ID was last in an event and the new i_2 player_ID was just in an event
j_2 = round2(j/2, 0)
mid_idx = round2((i_2*K + j_2*(1-K)), 0)
# update with this lineup going all the way back to the mid_idx play index
uq_players_df = as.data.frame(t(uq_players))
count = i_2 - mid_idx + 1
# and finally update the on court dataframe
uq_players_df = uq_players_df[rep(row.names(uq_players_df), count), ]
on_court[mid_idx:i_2, 1:5] = uq_players_df
}
# for the earliest plays of each half, before 5 player IDs were identified
# we left NAs in the on_court dataframe for whose on court.
# once we have 5 player IDs, drag them up to fill in the earlier NAs
this_row_NA = ifelse(is.na(on_court$P1[i_2]), 1, 0)
last_row_NA = ifelse(is.na(on_court$P1[(i_2-1)]), 1, 0)
if (last_row_NA > this_row_NA) {
NA_plays = which(is.na(on_court$P1))
start = min(NA_plays)
on_court[start:(i_2-1), 1:5] = on_court[i_2, 1:5]
}
}
那是我的代码。这有点复杂,因为即使只有514次播放,计数器i和j也会计数到1028,因此在索引播放次数时我必须使用i_2和j_2计数器。我引入mid_idx计数器进一步复杂化,我将其用于将玩家分组到游戏中,然后才能在游戏中进行标记。但是,我正在寻找帮助的主要问题是,这是完全在一个大的for循环中完成,有几个if情况,并且目前需要太多时间来运行(每场游戏约0.50秒)。我需要这个代码来运行成千上万的篮球游戏,并希望这个代码能够在1/10到1/100之间运行(可能无法实现)。矢量化代码,或使用应用函数,而不是大型for循环可能会使代码运行得更快,但我一直在努力思考它是如何实现的。今年夏天我一直在努力提高代码性能,并且努力改进这样的代码既困难又有用。如何加快速度,我们将不胜感激。
非常感谢!
答案 0 :(得分:0)
不幸的是,我认为你必须遍历数据帧的每一行。但是,我认为你在法庭上确定5名球员的算法可以更轻松。在我的解决方案中,我只迭代数据的行数,并使用长度为5的向量来控制场上的玩家:
for(i in 1:nrow(data)) {
# reset players on court
if(i == 1 | data$indicator_newperiod[i] == 1) poc = numeric(5)
# Add new Player1 (if any)
if(!is.na(data$player_id[i]) && !(data$player_id[i] %in% poc)) {
poc[1:4] = poc[2:5]
poc[5] = data$player_id[i]
}
# Add new Player2 (if any)
if(!is.na(data$player2_id[i]) && !(data$player2_id[i] %in% poc)) {
poc[1:4] = poc[2:5]
poc[5] = data$player2_id[i]
}
# fill result dataframe
on_court[i, 1:5] = poc
}
用于填写休息后的句点我向后迭代:
# Filling up players on court, after break
for(i in nrow(on_court):2) {
if(on_court$P1[i-1] == 0) on_court[i-1, 1:5] = on_court[i, 1:5]
}