Jesper.Lindberg
Jesper.Lindberg

Reputation: 341

Calculate difference between hours, not considering date

I have a vector with time stamps in the format:

"12:00:00"

it's 24 hour format and I do not have a date related to these timestamps. I would like to calculate the time difference between these.

So that the difference between 12 and 11 = 1, and the difference between 23 and 01 = 2.

I want to calculate the difference from time in the vector to a given time so that I get back a vector with the differences in time.

The data:

head(x3)
[1] "12:00:00" "18:00:00" "21:00:00" "06:00:00" "00:00:00" "09:00:00"

How would one approach this problem?

Upvotes: 0

Views: 316

Answers (4)

dom
dom

Reputation: 56

If you are only interested in the absolute difference of hours and the timestamp format is consistent you could do

absDif <- abs(as.numeric(substr(test_time[2], 1, 2)) - as.numeric(substr(test_time[1], 1, 2)))

If you also need the minutes, is is probably more convenient to generate a numeric time column first, e.g. like inside a loop

numericTimestamps[i] <- as.numeric(substr(test_time[i], 1, 2) + 
       as.numeric(substr(test_time[i], 4, 5) / 60 + 
       as.numeric(substr(test_time[i], 7, 8) / 60 / 60

EDIT to account for modified question:

If I get you right you are not looking for the absolute difference, but to the simple difference in hours considering a dayshift. In that case it might be more straight forward to go for dateTime objects as suggested below. If you want to stick with numerics you need to account for the date shift yourself, e.g. by adding an if condition:

absDif <- as.numeric(substr(test_time[2], 1, 2)) - as.numeric(substr(test_time[1], 1, 2))

# if the first is smaller than the second, there was most probably a shift in date
if(as.numeric(substr(test_time[2], 1, 2)) < as.numeric(substr(test_time[1], 1, 2))){

  # correct by 24 hours
  absDif <- as.numeric(substr(test_time[2], 1, 2)) - (as.numeric(substr(test_time[1], 1, 2)) + 24)
}

Upvotes: 2

G. Grothendieck
G. Grothendieck

Reputation: 270195

We assume that

  1. the inputs are strings in hh:mm:ss format where hh is between 00 and 23 inclusive
  2. only the hh should be used
  3. the output should be the hours as a number
  4. subtract the second hours from the first
  5. if the first hours is less than the second add 24 to #4

The above gives the following for the two examples in the question. No packages are used.

Diff <- function(t1, t2) {
  h1 <- as.numeric(sub(":.*", "", t1))
  h2 <- as.numeric(sub(":.*", "", t2))
  h1 - h2 + 24 * (h1 < h2)
}

Diff("12:00:00", "11:00:00")
## [1] 1

Diff("1:00:00", "23:00:00")
## [1] 2

If we wish to calculate the difference between v shown below and "12:00:00", say, then:

v <- c("12:00:00", "18:00:00", "21:00:00", "06:00:00", "00:00:00", "09:00:00")
Diff(v, "12:00:00")
## [1]  0  6  9 18 12 21

Upvotes: 2

Jake Kaupp
Jake Kaupp

Reputation: 8072

Here's an approach using the lubridate and dplyr

library(dplyr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

times <- data_frame(times = hms(c("12:00:00", "18:00:00", "21:00:00", "06:00:00", "00:00:00", "09:00:00")))

times %>% 
  mutate(difference =  hour(times) - lag(hour(times)))
#> # A tibble: 6 x 2
#>   times        difference
#>   <S4: Period>      <dbl>
#> 1 12H 0M 0S            NA
#> 2 18H 0M 0S             6
#> 3 21H 0M 0S             3
#> 4 6H 0M 0S            -15
#> 5 0S                   -6
#> 6 9H 0M 0S              9

Created on 2018-12-17 by the reprex package (v0.2.1)

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522636

One base R option would be to form makeshift date times using your data, by appending some arbitrary date. Then, use strptime and difftime to find the differences in hours:

t1 <- "12:00:00"
t2 <- "10:15:00"
date <- "2018-01-01"

as.numeric(difftime(strptime(paste(date, t1), "%Y-%m-%d %H:%M:%S"),
                    strptime(paste(date, t2), "%Y-%m-%d %H:%M:%S")))

[1] 1.75

Upvotes: 1

Related Questions