In my report, each line has an ID, a status and an activity datetime; from that, I need to figure out duration between two relevant statuses for one ID. For example, the data might be as shown below:
ID | status | activitydatetime
123456 | Account Ready | 04-25-2022 06:30
123456 | In Progress | 04-25-2022 06:30
123456 | Additional Documentation Needed | 04-25-2022 06:48
123456 | Completed | 04-25-2022 06:52
234567| In Progress | 04-23-2022 08:25
234567| Query Pending | 04-23-2022 08:30
234567 | In Progress | 04-25-2022 13:00
234567| Completed | 04-25-2022 13:17
From that data, I need to find the duration between In Progress and Completed. I'm using R for this but just can't figure out the best way to do this. The biggest hurdle is writing a script that will run by acct num for each status; i.e., "for 123456, datediff(status = In Progress, status = Completed)" and then go on to the next acct num - but a for loop doesn't feel like the cleanest way to do this.
I've managed to get this working:
library(dplyr)
# duration of all activity per account
activity <- df %>%
group_by(ID) %>%
summarize(
first_activity = min(activitydatetime),
last_activity = max(activitydatetime),
activity = last_activity - first_activity)
The issue is the report doesn't always start with status = In Progress, so I still need to be able to adjust my script to look at status...like, first_activity = the activitydatetime for the first time status = In Progress for ID 123456.
Unfortunately, doing the following gives me the literal first and last activitydatetime for the entire report.
activity <- df %>%
group_by(ID) %>%
summarize(
first_activity = df[first(which(df$status %in% "In Progress")),]$activitydatetime,
last_activity = df[last(which(df$status %in% "Completed")),]$activitydatetime,
activity = last_activity - first_activity)
Thanks for taking a look!