rdateunits-of-measurementpadr

How can I pad a vector of dates by units of less than 1 second in R?


I am trying to figure out how to add time intervals to a dates vector in R using units of less than 1 second. As you can see, padding one second intervals works, however decimals fail. I have looked around and it seems there is no "milliseconds" unit in R. Is there some way I can pad a vector of times with intervals of less than 1 second?

> require(padr)
> require(dplyr)
> options(digits.secs=3)
> start = as.POSIXct("2020-01-28 03:31:22.209 EST", format = "%Y-%m-%d %H:%M:%OS")
> end = as.POSIXct("2020-01-28 05:31:22.209 EST", format = "%Y-%m-%d %H:%M:%OS")
> 
> minz <- seq(start, end, units = "minutes", by = "1 min")
> head(minz)
[1] "2020-01-28 03:31:22.209 EST" "2020-01-28 03:32:22.209 EST" "2020-01-28 03:33:22.209 EST" "2020-01-28 03:34:22.209 EST"
[5] "2020-01-28 03:35:22.209 EST" "2020-01-28 03:36:22.209 EST"
> secz <- as.data.frame(minz) %>% pad('1 sec')
> head(secz)
                     minz
1 2020-01-28 03:31:22.209
2 2020-01-28 03:31:23.209
3 2020-01-28 03:31:24.209
4 2020-01-28 03:31:25.209
5 2020-01-28 03:31:26.209
6 2020-01-28 03:31:27.209
> half_secz <- as.data.frame(minz) %>% pad('0.5 sec')
Error in seq.int(0, to0 - from, by) : invalid '(to - from)/by'
> head(secz)
                     minz
1 2020-01-28 03:31:22.209
2 2020-01-28 03:31:23.209
3 2020-01-28 03:31:24.209
4 2020-01-28 03:31:25.209
5 2020-01-28 03:31:26.209
6 2020-01-28 03:31:27.209

Solution

  • We can use seq as it is specifying the by argument as 0.5.

    start = as.POSIXct("2020-01-28 03:31:22.209", format = "%Y-%m-%d %H:%M:%OS",tz = "UTC")
    end =  as.POSIXct("2020-01-28 05:31:22.209", format = "%Y-%m-%d %H:%M:%OS",tz = "UTC")
    
    seq(start, end, by = 0.5) %>% head
    
    #[1] "2020-01-28 03:31:22.209 UTC" "2020-01-28 03:31:22.709 UTC"
    #[3] "2020-01-28 03:31:23.209 UTC" "2020-01-28 03:31:23.709 UTC"
    #[5] "2020-01-28 03:31:24.209 UTC" "2020-01-28 03:31:24.709 UTC"
    

    Or if you want to use it in secz dataframe

    tidyr::complete(secz, minz = seq(min(minz), max(minz), by = 0.5))
    
    #    minz                   
    #   <dttm>                 
    # 1 2020-01-28 03:31:22.209
    # 2 2020-01-28 03:31:22.709
    # 3 2020-01-28 03:31:23.209
    # 4 2020-01-28 03:31:23.709
    # 5 2020-01-28 03:31:24.209
    # 6 2020-01-28 03:31:24.709
    # 7 2020-01-28 03:31:25.209
    # 8 2020-01-28 03:31:25.709
    # 9 2020-01-28 03:31:26.209
    #10 2020-01-28 03:31:26.709
    # … with 14,391 more rows