Given the following two character vectors composed of times in the format “%H:%M”:
Time1 <- c("23:00","23:59","00:01", "01:00") Time2 <- c("00:00"," 00:00","00:00", "00:00")
How can I compute the absolute distance between the times such that my results look like:
 60 1 1 60
with the values representing the number of minutes between each pair of times?
I've read documentation for the psych package and searched through documentation for lubridate, but have not identified a solution.
I attempted to assign a fixed date to the time vectors
fixedDATE <- "2012-05-19" T1 <- as.POSIXct(paste(fixedDATE, Time1 )) T2 <- as.POSIXct(paste(fixedDATE, Time1 )) T1 - T2 Time differences in mins  1380 1439 1 60
I appreciate any help in solving this seemingly simple dilemma.
Here's how you can use lubridate:
library(lubridate); x <- abs(period_to_seconds(hm(Time1)-hm(Time2)))/60; ifelse(x>720,1440-x,x); ##  60 1 1 60
Your expected result for this new example is at odds with the example given in your question.
Because of the cyclical nature of the 24 hour clock, for two plain times, there are two possible positive distances you could compute between the two plain times: (1) cycling the clock forward from Time1 to Time2, or (2) cycling the clock backward from Time1 to Time2. One of the computations will require conceptually "wrapping" the distance around an endpoint (00:00 or 24:00), and the other computation will not require wrapping. Which endpoint is crossed, and which computation crosses it, depends on the absolute order of the two times. For Time1>Time2, the forward computation will wrap around 24:00 while the backward computation will not wrap, whereas for Time1<Time2, the forward computation will not wrap while the backward computation will wrap around 00:00.
Except for the special cases of (1) equal times and (2) distances of 12 hours, all (Time1,Time2) pairs will produce a different distance for the backward computation as compared to the forward computation.
Based on the example you gave in your question, it appeared that you wanted to select the smaller distance of the two possible distances that can be attained from the two types of computation. For that example, the forward distances are 60,1,1439,1380, while the backward distances are 1380,1439,1,60. Since you wanted 60,1,1,60 as the result, it certainly looks like you wanted the smaller of the two possible distances.
This is why IaroslavDomin offered a solution using
pmin() to get the smaller distance. I used
ifelse() to achieve the same logic.
For your new example of Time1=18:00 and Time2=07:00, the forward distance is 780 (that's 13 hours), while the backward distance is 660 (that's 11 hours). Using the same code that I and IaroslavDomin wrote, we get the result of 660, which is what we would expect, given the intention of selecting the smaller of the two possible distances.
If you have a different requirement in mind, you should ask a new question specifying the different requirement. But given the interpretation of selecting the smaller of the two possible distances, both my code and IaroslavDomin's code are correct.