I'm working with EHR data and trying to identify the time when an individual identified by ID has had at least 2 unique events of type "A" and at least 1 unique event of type "B" within a 6hr time range. The order of events does not matter - the 6hr range can start with either type "A" or "B". I would like to create a new data frame that contains the timestamp when the individual meets the criteria.
The data looks like this:
d <- data.frame(ID = c("Z001","Z001","Z001","Z001","Z001","Z001"),
event = c("TEMP","HR","TEMP","RR","LACTATE","INR"),
eventType = c("A","A","A","A","B","B"),
eventDTS = as_datetime(c("2022-06-01T02:00:00Z","2022-06-01T02:00:00Z","2022-06-01T02:05:00Z","2022-06-01T02:01:00Z","2022-06-01T03:00:00Z","2022-06-01T03:45:00Z")),
stringsAsFactors=FALSE)
ID | event | eventType | eventDTS |
---|---|---|---|
Z001 | TEMP | A | 2022-06-01 02:00:00 |
Z001 | HR | A | 2022-06-01 02:00:00 |
Z001 | RR | A | 2022-06-01 02:01:00 |
Z001 | TEMP | A | 2022-06-01 02:05:00 |
Z001 | LACTATE | B | 2022-06-01 03:00:00 |
Z001 | INR | B | 2022-06-01 03:45:00 |
For this individual the output should look like this:
ID | lastQualDTS |
---|---|
Z001 | 2022-06-01 03:00:00 |
So far I've been able to create a count of events within the 6hr range for each event using this post R to create a tally of previous events within a sliding window time period. But I can't figure out how to actually identify the timestamp of interest.