jerry han
jerry han

Reputation: 435

how to calculate time interval and divide it by integer in r?

i have time string as "08:00","06:00" and i wanna calculate difference between them and divide it by 15 mins.

then results should be 8 in integer i don't how to code in R

anybody can help me ?

Upvotes: 1

Views: 418

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389325

It would be easy with lubridate, where we convert the strings in hm format and divide by 15 minutes.

library(lubridate)
(hm(a) - hm(b))/minutes(15)
#[1] 8

data

a <- "08:00"
b <- "06:00"

Upvotes: 1

Maurits Evers
Maurits Evers

Reputation: 50738

Something like this using difftime?

difftime(
    as.POSIXct("08:00", format = "%H:%M"), 
    as.POSIXct("06:00", format = "%H:%M"), 
    units = "mins") / 15
#Time difference of 8 mins

Or to convert to numeric

as.numeric(
    difftime(as.POSIXct("08:00", format = "%H:%M"), 
    as.POSIXct("06:00", format = "%H:%M"), 
    units = "mins") / 15)
#[1] 8

Upvotes: 2

Related Questions