且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

插入缺失日期/时间的行

更新时间:2023-02-06 13:58:18

我认为最简单的方法是先按照已经描述的方式设置 Date,然后转换为 zoo,然后再设置一个合并:

I think the easiest thing ist to set Date first as already described, convert to zoo, and then just set a merge:

df$timestamp<-as.POSIXct(df$timestamp,format="%m/%d/%y %H:%M")

df1.zoo<-zoo(df[,-1],df[,1]) #set date to Index

df2 <- merge(df1.zoo,zoo(,seq(start(df1.zoo),end(df1.zoo),by="min")), all=TRUE)

开始和结束是从您的 df1(原始数据)中给出的,并且您正在设置 - 例如 min - 根据您的示例需要.all=TRUE 将缺失日期的所有缺失值设置为 NA.

Start and end are given from your df1 (original data) and you are setting by - e.g min - as you need for your example. all=TRUE sets all missing values at the missing dates to NAs.