Extracting time from POSIXct

后端 未结 6 672
梦如初夏
梦如初夏 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:04

    The data.table package has a function 'as.ITime', which can do this efficiently use below:

    library(data.table)
    x <- "2012-03-07 03:06:49 CET"
    as.IDate(x) # Output is "2012-03-07"
    as.ITime(x) # Output is "03:06:49"
    
    0 讨论(0)
  • 2020-11-28 23:09

    There have been previous answers that showed the trick. In essence:

    • you must retain POSIXct types to take advantage of all the existing plotting functions

    • if you want to 'overlay' several days worth on a single plot, highlighting the intra-daily variation, the best trick is too ...

    • impose the same day (and month and even year if need be, which is not the case here)

    which you can do by overriding the day-of-month and month components when in POSIXlt representation, or just by offsetting the 'delta' relative to 0:00:00 between the different days.

    So with times and val as helpfully provided by you:

    ## impose month and day based on first obs
    ntimes <- as.POSIXlt(times)    # convert to 'POSIX list type'
    ntimes$mday <- ntimes[1]$mday  # and $mon if it differs too
    ntimes <- as.POSIXct(ntimes)   # convert back
    
    par(mfrow=c(2,1))
    plot(times,val)   # old times
    plot(ntimes,val)  # new times
    

    yields this contrasting the original and modified time scales:

    enter image description here

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-28 23:18

    You can use strftime to convert datetimes to any character format:

    > t <- strftime(times, format="%H:%M:%S")
    > t
     [1] "02:06:49" "03:37:07" "00:22:45" "00:24:35" "03:09:57" "03:10:41"
     [7] "05:05:57" "07:39:39" "06:47:56" "07:56:36"
    

    But that doesn't help very much, since you want to plot your data. One workaround is to strip the date element from your times, and then to add an identical date to all of your times:

    > xx <- as.POSIXct(t, format="%H:%M:%S")
    > xx
     [1] "2012-03-23 02:06:49 GMT" "2012-03-23 03:37:07 GMT"
     [3] "2012-03-23 00:22:45 GMT" "2012-03-23 00:24:35 GMT"
     [5] "2012-03-23 03:09:57 GMT" "2012-03-23 03:10:41 GMT"
     [7] "2012-03-23 05:05:57 GMT" "2012-03-23 07:39:39 GMT"
     [9] "2012-03-23 06:47:56 GMT" "2012-03-23 07:56:36 GMT"
    

    Now you can use these datetime objects in your plot:

    plot(xx, rnorm(length(xx)), xlab="Time", ylab="Random value")
    

    enter image description here


    For more help, see ?DateTimeClasses

    0 讨论(0)
  • 2020-11-28 23:19

    Many solutions have been provided, but I have not seen this one, which uses package chron:

    hours = times(strftime(times, format="%T"))
    plot(val~hours)
    

    (sorry, I am not entitled to post an image, you'll have to plot it yourself)

    0 讨论(0)
  • 2020-11-28 23:28

    I can't find anything that deals with clock times exactly, so I'd just use some functions from package:lubridate and work with seconds-since-midnight:

    require(lubridate)
    clockS = function(t){hour(t)*3600+minute(t)*60+second(t)}
    plot(clockS(times),val)
    

    You might then want to look at some of the axis code to figure out how to label axes nicely.

    0 讨论(0)
提交回复
热议问题