I have a data set in R with a series of people, events that occur and an assigned time that they occur in seconds, starting from 0. It looks similar to this:
event seconds person
1 0.0 Bob
2 15.0 Bob
3 28.5 Bob
4 32.0 Joe
5 38.0 Joe
6 41.0 Joe
7 42.5 Joe
8 55.0 Anne
9 58.0 Anne
I need to filter for each name, and that means the ordered events will not be sequential for each person.
An example of what this looks like (notice how Bob is not involved in events 4-40, etc.):
event seconds person
1 0.0 Bob
2 15.0 Bob
3 28.5 Bob
41 256.0 Bob
42 261.0 Bob
43 266.0 Bob
44 268.5 Bob
45 272.0 Bob
46 273.0 Bob
49 569.0 Bob
80 570.5 Bob
81 581.0 Bob
The events that are sequential and related are separated by an increment of 1. I would like to find the duration of the related events, for example, events 1-3 is a group that would be 28.5 seconds. Events 41-46 is another group that lasts 17 seconds. This would be required for all the names that are listed in the person column.
I have tried filtering the names using dplyr and then finding the difference between event rows, using as.matrix, and determining where the increment is greater than 1 (indicating it's no longer part of the current sequence of events). I haven't found a way to assign the max and min based off of this to determine the duration of related events. The solution does not need to involve this step though, but it was the closest I could come.
The end goal is to plot the non-contiguous time durations for each person to have a visual representation of each person's event involvement for the entire data set.
Thank you in advance.