Extracting time from POSIXct

后端 未结 6 719
梦如初夏
梦如初夏 2020-11-28 23:02

How would I extract the time from a series of POSIXct objects discarding the date part?

For instance, I have:

times <- structure(c(1331086009.5009         


        
6条回答
  •  星月不相逢
    2020-11-28 23:16

    The time_t value for midnight GMT is always divisible by 86400 (24 * 3600). The value for seconds-since-midnight GMT is thus time %% 86400.

    The hour in GMT is (time %% 86400) / 3600 and this can be used as the x-axis of the plot:

    plot((as.numeric(times) %% 86400)/3600, val)
    

    enter image description here

    To adjust for a time zone, adjust the time before taking the modulus, by adding the number of seconds that your time zone is ahead of GMT. For example, US central daylight saving time (CDT) is 5 hours behind GMT. To plot against the time in CDT, the following expression is used:

    plot(((as.numeric(times) - 5*3600) %% 86400)/3600, val)
    

提交回复
热议问题