Rounding time to nearest quarter hour

前端 未结 7 622
清酒与你
清酒与你 2020-12-03 13:43

I have a vector of POSIXct values and I would like to round them to the nearest quarter hour. I don\'t care about the day. How do I convert the values to hours and minutes?<

7条回答
  •  無奈伤痛
    2020-12-03 14:09

    You can use the align.time function in the xts package to handle the rounding, then format to return a string of "HH:MM":

    R> library(xts)
    R> p <- as.POSIXct("2012-05-30 20:41:21", tz="UTC")
    R> a <- align.time(p, n=60*15)  # n is in seconds
    R> format(a, "%H:%M")
    [1] "20:45"
    

提交回复
热议问题