我有几个小时的时间。
例如:
vec.hours <- c("15:52:00", "15:56:00", "12:10:00", "15:12:00", "11:49:00" ,"13:35:00", "14:53:00")
我想四舍五入以获得新的小时数,这将是最近的整整5分钟,
像这样。
round.hours <- c("15:50:00", "16:00:00", "12:10:00", "15:10:00", "11:50:00" ,"13:35:00", "14:55:00" )
我试过了
hour <- strptime(vec.hours , "%H:%M:%S")
round.hour <- round(hour , "mins")
但这不起作用。
在每一个hours.hours之后,我想做+/-一小时,例如这样:
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")
因此,对于15:50:00,我有一个从14:50到16:50的时间向量。
我不知道如何从vec.hours获得round.hour。
非常感谢
最佳答案
我将把小时数放入datetime对象中,将其转换为POSIXlt
,这使您可以将整数作为分钟来访问分钟,使用整数除法取整,然后再次提取小时数。
timestamps <- as.POSIXlt(as.POSIXct('1900-1-1', tz='UTC') + as.difftime(vec.hours))
timestamps$min <- (timestamps$min + 5/2) %/% 5 * 5
format(timestamps, format='%H:%M:%S')
# [1] "15:50:00" "15:55:00" "12:10:00" "15:10:00" "11:50:00" "13:35:00" "14:55:00"
关于r - 如何舍入时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108515/