我有一个数据集,其中一列记录秒,其他列记录该字段上的当前玩家。例如,它看起来像,
Time.sec. MTL1 MTL2 MTL3
1 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
2 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
3 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
4 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
5 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
6 11-R BRENDAN GALLAGHER 14-C TOMAS PLEKANEC 31-G CAREY PRICE
大约有10名球员,但在任何特定时间只有3名球员。如果一名球员加入MTL1,他可以在未来某个未指定的时间离开并返回3 MTL中的任何一个。
我想创建一个看起来像这样的数据框,覆盖每个玩家的时间段。
Player ShiftStart ShiftEnd
11-R BRENDAN GALLAGHER 1 9
11-R BRENDAN GALLAGHER 50 64
11-R BRENDAN GALLAGHER 100 107
我该怎么做呢?
编辑:添加样本输入
sample <- structure(list(Time.sec. = 1:75, MTL1 = c("11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER", "11-R BRENDAN GALLAGHER",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN"), MTL2 = c("14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC",
"14-C TOMAS PLEKANEC", "14-C TOMAS PLEKANEC", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN", "15-L TOMAS FLEISCHMANN",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE", "22-R DALE WEISE",
"22-R DALE WEISE"), MTL3 = c("31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY", "26-D JEFF PETRY",
"26-D JEFF PETRY"), MTL4 = c("67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "67-L MAX PACIORETTY", "67-L MAX PACIORETTY",
"67-L MAX PACIORETTY", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE",
"31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE", "31-G CAREY PRICE"
), MTL5 = c("76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS", "51-C DAVID DESHARNAIS",
"51-C DAVID DESHARNAIS"), MTL6 = c("79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "79-D ANDREI MARKOV", "79-D ANDREI MARKOV",
"79-D ANDREI MARKOV", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN",
"76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "76-D P.K. SUBBAN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN",
"74-D ALEXEI EMELIN", "74-D ALEXEI EMELIN"), TOR1 = c("2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK", "2-D MATT HUNWICK",
"2-D MATT HUNWICK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK"), TOR2 = c("3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF", "3-D DION PHANEUF",
"3-D DION PHANEUF", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES"), TOR3 = c("19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL", "19-R JOFFREY LUPUL",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK", "21-L JAMES VAN RIEMSDYK",
"21-L JAMES VAN RIEMSDYK", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON",
"36-D SCOTT HARRINGTON", "36-D SCOTT HARRINGTON"), TOR4 = c("23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS", "23-C SHAWN MATTHIAS",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES", "28-R BRAD BOYES",
"28-R BRAD BOYES", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI"), TOR5 = c("42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK", "42-C TYLER BOZAK",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI", "43-C NAZEM KADRI",
"43-C NAZEM KADRI", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER"), TOR6 = c("45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER", "45-G JONATHAN BERNIER",
"45-G JONATHAN BERNIER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER", "51-D JAKE GARDINER",
"51-D JAKE GARDINER", "51-D JAKE GARDINER")), .Names = c("Time.sec.",
"MTL1", "MTL2", "MTL3", "MTL4", "MTL5", "MTL6", "TOR1", "TOR2",
"TOR3", "TOR4", "TOR5", "TOR6"), row.names = c(NA, 75L), class = "data.frame")
答案 0 :(得分:2)
这是一个避免使用for循环的解决方案。我创建了一个简单的通用移位数据框架进行测试。
players <- c("Player 1", "Player 2", "Player 3", "Player 4")
times <- 1:10
spot1 <- c(rep('Player 1', 5), rep('Player 2', 5))
spot2 <- c(rep('Player 2', 1), rep('Player 3', 2), rep('Player 4', 6),rep('Player 1',1))
shift_df <- data.frame(times, spot1, spot2)
shift_df
player_on_ice函数返回一个逻辑向量,指示玩家在给定时间点在冰上。
player_on_ice <- function(shift_df, player_name) {
apply(shift_df, 1, function(x) {is.element(player_name, x)}
)}
函数shift_calculator获取上面的shift data.frame和一个玩家名称并计算他们的移位。它依靠rle
功能来跟踪玩家在冰上的时间以及持续时间。
shift_calculator <- function(shift_df, player_name) {
on_ice <- player_on_ice(shift_df, player_name)
# check to see if player entered
if (sum(on_ice) > 0) {
# rle = 'running length encoding'
# returns values which indicates when player is on/off ice and
# how long they were on or off
shifts <- rle(on_ice)
# use cumsum to see they switch from on/off
switches <- c(0, cumsum(shifts$lengths))
# use lapply to find when they were on/off
shifts <- lapply(1:length(shifts$values), function(x) {
if (shifts$values[x]) {
data.frame(shiftStart=switches[x]+1, shiftEnd=switches[x+1])
} else {
NULL
}})
player_df <- do.call(rbind, shifts)
data.frame(player=player_name, player_df)
}
}
迭代玩家姓名,然后生成最终数据框,其中包含玩家名称并移动开始和结束。
do.call(rbind, lapply(players, function(x) shift_calculator(shift_df, x)))
答案 1 :(得分:1)
所以,我认为这是有效的。我有一个更简单的解决方案,但我害怕玩家离开游戏的情况,然后在同一位置重新进入游戏(比如玩家1离开MTL1,然后返回MTL1)。我创建了一个新的变量GSI(游戏状态标识符),从1开始,然后每次游戏状态改变(玩家离开或进入等)时增加。这个应该避免summarise
的棘手行为,因为我使用min
来提供开始时间,max
来给出结束时间。如果没有GSI并且考虑到上面我担心的情况,我认为min
会给MTL1的第一个班次开始,max
会给MTL1的第二个班次结束,因此不会认识到这是两个截然不同的转变。如果有什么不起作用,请告诉我。
library(dplyr)
library(tidyr)
#you will have to change gather_cols to suit your needs. It should be all columns in your
#data except Time.sec.
long<-sample %>% gather(pos, player, gather_cols = MTL1:TOR6)
long$GSI<-1
num<-1
## this might be a little slow if you have a lot of players.
for(i in 1:nrow(long)-1){
if(isTRUE(all.equal(long[i,]$pos, long[i+1,]$pos)) &&
isTRUE(all.equal(long[i,]$player, long[i+1,]$player))){
long[i+1,]$GSI<-num
}
else {
num<-num+1
long[i+1,]$GSI<-num
}
}
long %>% group_by(player, pos, GSI) %>%
summarise(shiftStart = min(Time.sec.), shiftStop = max(Time.sec.))
player pos GSI shiftStart shiftStop
(chr) (chr) (dbl) (int) (int)
1 11-R BRENDAN GALLAGHER MTL1 2 1 46
2 14-C TOMAS PLEKANEC MTL2 4 1 36
3 15-L TOMAS FLEISCHMANN MTL1 3 47 75
4 15-L TOMAS FLEISCHMANN MTL2 5 37 46
5 19-R JOFFREY LUPUL TOR3 22 1 37
6 2-D MATT HUNWICK TOR1 18 1 50
7 21-L JAMES VAN RIEMSDYK TOR1 19 51 75
8 21-L JAMES VAN RIEMSDYK TOR3 24 43 50
9 22-R DALE WEISE MTL2 6 47 75
10 23-C SHAWN MATTHIAS TOR3 23 38 42
.. ... ... ... ... ...