Fill missing date-time values in output vector

不想你离开。 提交于 2019-12-08 04:34:07

问题


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.


回答1:


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".



来源:https://stackoverflow.com/questions/39525750/fill-missing-date-time-values-in-output-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!