Exc
Exc

Reputation: 183

Recording time periods of repeated events

I have a dataset where one column records seconds and the others record current players on the field. So for example , it looks like ,

       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

There are about 10 players but only 3 can be there at any given time. If a player joins at MTL1 he could leave at some unspecified time in the future and return in say any of the 3 MTL's.

I want to create a dataframe that looks like this that covers each players time periods inside.

          Player             ShiftStart ShiftEnd
 11-R BRENDAN GALLAGHER          1        9
 11-R BRENDAN GALLAGHER         50       64
 11-R BRENDAN GALLAGHER        100      107

How would I go about doing this?

EDIT : added sample dput

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")

Upvotes: 0

Views: 86

Answers (2)

jrd
jrd

Reputation: 46

Here's a solution that avoids using for loops. I created a simple generic shift data.frame for testing.

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

The player_on_ice function returns a logical vector indicating the player was on the ice at a give time point.

player_on_ice <- function(shift_df, player_name) {
    apply(shift_df, 1, function(x) {is.element(player_name, x)}
)}

The function shift_calculator takes the shift data.frame above and a player name and calculates their shifts. It relies on the rle function to keep track of when the player's on the ice and for how long.

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)
    }
}

Iterating over player names then produces your final data frame with player name and shift beginnings and ends.

do.call(rbind, lapply(players, function(x) shift_calculator(shift_df, x)))

Upvotes: 2

Bryan Goggin
Bryan Goggin

Reputation: 2489

So, I think this works. I had a simpler solution but I was afraid of the situation where a player leaves the game, then re-enters the game in the same position (say player 1 leaves MTL1, then returns to MTL1 later). I created a new variable GSI (game-state-identifier) that starts at 1 then increases each time a game-state changes (player leaves or enters, etc). This should avoid screwy behavior by summarise since I am using it with min to give the start time, and max to give the end time. Without GSI and given the situation that I am worried about above, I think min would give the start of the first shift at MTL1 and max would give the end of the second shift at MTL1, and thus not recognize that those are two distinct shifts. Let me know if something doesn't work.

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
..                     ...   ...   ...        ...       ...

Upvotes: 1

Related Questions