0

The dataframe below contains timestamps related to when an incident is logged and resolved. Vector ResolutionDiff contains the difference in minutes fromthe Resolved and Logged vectors as calculated by the following statement:

dataset$ResolutionDiff <- difftime(dataset$Resolved, dataset$Logged)

| Reportnumber | Priority | Logged          | Resolved        | ResolutionDiff     | netdifftime |
|--------------|----------|-----------------|-----------------|--------------------|-------------|
| 1            | High     | 22/1/2016 17:52 | 25/1/2016 13:35 | 406288333333333,00 |             |
| 2            | Medium   | 18/1/2016 13:09 | 22/1/2016 12:55 | 5745.7             |             |
| 3            | Medium   | 15/1/2016 10:47 | 18/1/2016 13:06 | 445876666666667,00 |             |
| 4            | Medium   | 15/1/2016 10:32 | 18/1/2016 13:04 | 447173333333333,00 |             |
| 5            | High     | 14/1/2016 14:13 | 14/1/2016 15:33 | 792333333333333,00 |             |

I am trying to calculate the vector netdifftime by conditionally subtracting non-working hours from ResolutionDiff, such as weekends, when dataset$Priority !=High. For example, for row 1, the expected output in netdifftime would be 4062.883 - (48*60) = 1183.

Is there a way to do such a task in R? I have been digging into libraries like lubridate and suspect that a possible solution would be to create a temporary variable with weekend intervals which could then be checked against the corresponding interval for dataset$Resolved, dataset$Logged, but have not managed to get something to work.

3
  • Yes, it is possible to do this. You just need to thoroughly define your rules. ...Also, FYI- dput your example data vs (or in addition to) pasting it. Jan 26, 2016 at 16:50
  • An approach that might be not memory efficient seems to be dataset$ResolutionDiff - mapply(function(d1, d2) sum(as.POSIXlt(seq(d1, d2, "day"))$wday %in% c(0L, 6L)) * (24 * 60), dataset$Logged, dataset$Resolved). It might help to post a more complete example.
    – alexis_laz
    Jan 26, 2016 at 19:16
  • It will actually be a bit more complicated, as the net difference should also take into account whether the incident was logged during or outside business hours, similar to excel's NETWORKDAYS function. I'm currently working on an approach, so I;ll also update the original question accordingly.
    – orestisf
    Jan 31, 2016 at 18:55

1 Answer 1

-1

I ran in to the same question and ended up writing a function for that, and later build a package for it.
https://github.com/janzzon/difftimeOffice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.