I have a large dataset that looks like below.
Date Time PeopleExposed
11/1 1PM 1
11/1 3PM 0
11/2 1PM 5
11/2 4PM 10
11/2 6PM 3
11/2 7PM 0
11/2 8PM 2
11/2 9PM 0
11/3 3PM 5
11/3 6PM 0
PeopleExposed indicates the number of people exposed to a chemical. So going from 0 to a positive number of people means an exposure event happened, then back to 0 means it has ended.
How would I calculate, for each day, the total number of unique "events" and "people-hours of exposure"?
So for # "events", there are 1 for 11/1, 2 for 11/2, and 1 for 11/3.
And for "people-hours":
Event #1, 11/1: 1P is exposed for 2 hours (1PM to 3PM): 2 people-hours.
Event #1, 11/2: 5P are exposed for 3hrs, then 10P are exposed for 2hrs, then 3P are exposed for 1hr: 38 people-hours.
Event #2, 11/2: 2P are exposed for 1hr: 2 people-hours.
Event #1, 11/3: 5P are exposed for 3hrs: 15 people-hours
I want to use data.table or dplyr to return:
Date NumEvents PeopleHoursTotal
11/1 1 2
11/2 2 40
11/3 1 15
and
Date EventNum PeopleHoursTotal TimeStart TimeEnd
11/1 1 2 1PM 3PM
11/2 1 38 1PM 7PM
11/2 2 2 8PM 8PM
11/3 1 15 3PM 6PM
I've been creating indicator variables to create indicator variable columns for start and end of an event, but not sure what to do after that.