Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round a time?

Tags:

time

r

I have a vector of hours. For example:

vec.hours <- c("15:52:00", "15:56:00", "12:10:00", "15:12:00",  "11:49:00" ,"13:35:00", "14:53:00")

I would like to round the hours to get the new hours that will be the nearest whole 5 minutes, like this.

round.hours <- c("15:50:00", "16:00:00", "12:10:00", "15:10:00",  "11:50:00" ,"13:35:00", "14:55:00" )

I tried this

hour <- strptime(vec.hours , "%H:%M:%S")
round.hour <- round(hour , "mins")

But it isn't working.

After for each round.hours I want to do +/- one hour, for example like this:

hour.rd <- strptime(round.hours[1] , "%H:%M:%S")
hourM <- hour.rd - 3600
hourP <- hour.rd + 3600
l.tm <- timeSequence(from = hourM, to = hourP,format = "%H-%S-%M",by="5 min",FinCenter = "Europe/Zurich")

so, for 15:50:00 I have a vector of times from 14:50 until 16:50.

I dont know how to get round.hour from vec.hours.

Many thanks

like image 759
Tali Avatar asked Jun 14 '13 12:06

Tali


People also ask

How do I round time?

When reporting time not worked, employees should round to the nearest 1/4 hour. For example: 15 minutes = 0.25 hours, 30 minutes = 0.50 hours and 45 minutes = 0.75 hours. Examples: The employee leaves early due to illness 1 hour and 45 minutes before the end of the day.

How do you round time to the nearest minute?

You also can use this formula =MROUND(A2,15/60/24) to round time to nearest minute.

How do you round minutes to hours?

How to round to the nearest hour. If the clock shows 30 or more minutes, round upward to the next hour. If the clock shows less than 30 minutes, round down to the current hour.


Video Answer


1 Answers

The Lubridate package has the very friendly round_date function.

round_date(hour,unit="5 minutes")

[1] "2017-08-28 15:50:00 UTC" "2017-08-28 15:55:00 UTC"
[3] "2017-08-28 12:10:00 UTC" "2017-08-28 15:10:00 UTC"
[5] "2017-08-28 11:50:00 UTC" "2017-08-28 13:35:00 UTC"
[7] "2017-08-28 14:55:00 UTC"
like image 136
groceryheist Avatar answered Oct 08 '22 17:10

groceryheist