r: complete value in missing date

你说的曾经没有我的故事 提交于 2019-12-24 03:55:07

问题


In R, If I have this data

           date.hour temp
2014-01-05 20:00:00            16
2014-01-06 20:00:00            14
2014-01-06 22:00:00            18

and with seq I can get a sequence of date time

begin <- as.POSIXct('2014-1-5')
end <- as.POSIXct('2014-1-7')
seq(begin, end, by=2*3600)

how I can complete the data to something similar to

           date.hour temp
2014-01-05 00:00:00            NA
2014-01-05 02:00:00            NA
...
2014-01-05 18:00:00            NA
2014-01-05 20:00:00            16
2014-01-05 22:00:00            NA
...
2014-01-06 20:00:00            18
2014-01-06 22:00:00            14
...
2014-01-07 00:00:00            NA

回答1:


If this is your sample data frame

dd<-data.frame(
    date.hour = structure(c(1388970000, 1389056400, 1389063600), class = c("POSIXct", "POSIXt"), tzone = ""),
    temp = c(16L, 14L, 18L)
)

then you can just merge() that with your sequence

begin <- as.POSIXct('2014-1-5')
end <- as.POSIXct('2014-1-7')
comp<-seq(begin, end, by=2*3600)

merge(data.frame(date.hour=comp), dd, all.x=T)

by setting all.x=T, missing values will be filled with NA.




回答2:


Or similarly with data.table

Your data (from @rawr)

df <- read.table(header = TRUE, text = "date.hour temp
'2014-01-05 20:00:00'            16
'2014-01-06 20:00:00'            14
'2014-01-06 22:00:00'            18", colClasses = c('POSIXct','numeric'))

Solution

library(data.table)
dt <- data.table(date.hour = seq(begin, end, by=2*3600))
setkey(setDT(df), date.hour)
df[dt]
#               date.hour temp
#  1: 2014-01-05 00:00:00   NA
#  2: 2014-01-05 02:00:00   NA
#  3: 2014-01-05 04:00:00   NA
#  4: 2014-01-05 06:00:00   NA
#  5: 2014-01-05 08:00:00   NA
#  6: 2014-01-05 10:00:00   NA
#  7: 2014-01-05 12:00:00   NA
#  8: 2014-01-05 14:00:00   NA
#  9: 2014-01-05 16:00:00   NA
# 10: 2014-01-05 18:00:00   NA
# 11: 2014-01-05 20:00:00   16
# 12: 2014-01-05 22:00:00   NA
# 13: 2014-01-06 00:00:00   NA
# 14: 2014-01-06 02:00:00   NA
# 15: 2014-01-06 04:00:00   NA
# 16: 2014-01-06 06:00:00   NA
# 17: 2014-01-06 08:00:00   NA
# 18: 2014-01-06 10:00:00   NA
# 19: 2014-01-06 12:00:00   NA
# 20: 2014-01-06 14:00:00   NA
# 21: 2014-01-06 16:00:00   NA
# 22: 2014-01-06 18:00:00   NA
# 23: 2014-01-06 20:00:00   14
# 24: 2014-01-06 22:00:00   18
# 25: 2014-01-07 00:00:00   NA


来源:https://stackoverflow.com/questions/29152654/r-complete-value-in-missing-date

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