r:缺少日期的完整值 [英] r: complete value in missing date

查看:77
本文介绍了r:缺少日期的完整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,如果我有此数据

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

,并且使用seq我可以获得一个日期时间序列

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)

我如何完成类似于

           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

推荐答案

如果这是您的示例数据框

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)
)

然后您只需按顺序merge()即可

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)

通过设置all.x=T,缺失的值将用NA填充.

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

这篇关于r:缺少日期的完整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆