user3919790
user3919790

Reputation: 557

Substracting minutes from time (in 24 hour format)?

Was wondering if some could help

time <-c("16:09", "05:26", "08:47", "03:04", "03:07", "02:01")
min <-c(60, 5, 124, 35, 400, 2)

I want to extract a revised time with nth element of vector "min" subtracted from the nth element of vector "time". So for example the 1st element of vector "revised.time" should be 15.09 after subtracting 60 mins.

Thanks in advance

A

Upvotes: 0

Views: 61

Answers (3)

JasonAizkalns
JasonAizkalns

Reputation: 20483

This should give you what you're looking for, but you may want to consider a dedicated package:

time <- c("16:09", "05:26", "08:47", "03:04", "03:07", "02:01")
min  <- c(60, 5, 124, 35, 400, 2)

format(strptime(time, "%M:%S") - min, "%M:%S")
# [1] "15:09" "05:21" "06:43" "02:29" "56:27" "01:59"

Upvotes: 2

GPierre
GPierre

Reputation: 903

You need to format your time vector first. Then you can substract seconds like this:

time2<-strptime(time, "%H:%M")
time2[1]-(60*60)

[1] "2015-10-19 15:09:00 CEST"

Upvotes: 0

bramtayl
bramtayl

Reputation: 4024

You can do this in lubridate

library(lubridate)

hm(time) - new_period(minute = min)

Upvotes: 0

Related Questions