Search code examples
rtime-seriesuniquemissing-data

Fill missing date-time values in output vector


I performed a unique extraction on my date-time values for extracting unique seconds in my time-series data.

unique_seconds <- unlist(unique(all_secondsDayData))

I have missing values in my output as follows:

(see "2015-12-03 09:51:26")

[1116] "2015-12-03 09:51:24" "2015-12-03 09:51:25" "2015-12-03 09:51:27" "2015-12-03 09:51:28" "2015-12-03 09:51:29"

How do I fill my vector with these missing values? Do not worry about date because it is one day's data. The issue is filling the vector with missing seconds values.


Solution

  • Looks like you just want a regular time sequence by 1 second. You may try using seq.POSIXt:

    x <- c("2015-12-03 09:51:24", "2015-12-03 09:51:25", "2015-12-03 09:51:27",
           "2015-12-03 09:51:28", "2015-12-03 09:51:29")
    
    x <- strptime(x, "%Y-%m-%d %H:%M:%S")
    
    RANGE <- range(x)
    seq.POSIXt(RANGE[1], RANGE[2], by = 1)
    
    #[1] "2015-12-03 09:51:24 GMT" "2015-12-03 09:51:25 GMT"
    #[3] "2015-12-03 09:51:26 GMT" "2015-12-03 09:51:27 GMT"
    #[5] "2015-12-03 09:51:28 GMT" "2015-12-03 09:51:29 GMT"
    

    You can read ?seq.POSIXt for specification of by. If it is given as a single number, it is interpreted as "second".