data.table:按小时增加IDateTime以延迟每小时数据

时间:2015-01-08 10:31:42

标签: r date data.table

如何将data.table的idate和itime增加一小时?

我想按照Christoph_J所描述的here来延迟我的数据。

我的数据看起来像这样

> dt
             idate    itime windgeschwindigkeit
     1: 1958-02-01 00:00:00          -0.9049475
     2: 1958-02-01 01:00:00          -0.9049475
     3: 1958-02-01 02:00:00          -0.9049475
     4: 1958-02-01 03:00:00          -1.0049475
     5: 1958-02-01 04:00:00          -2.0049475
    ---                                        
498020: 2014-11-24 19:00:00          -1.0852256
498021: 2014-11-24 20:00:00          -0.7852256
498022: 2014-11-24 21:00:00          -0.8852256
498023: 2014-11-24 22:00:00          -1.0852256
498024: 2014-11-24 23:00:00          -1.3852256

我试图用上面提到的SO答案中的代码来延迟它,如下所示:

setkeyv(dt, c("idate","itime"))
m_col = "windgeschwindigkeit"
pm_col = parse(text="windgeschwindigkeit")
lagg = 1
dt[, paste0(m_col,"_",lagg) :=
     dt[list(idate,itime+lagg*3600), eval(pm_col), roll=-1]]

导致预期的输出:
一个新列落后一小时。 (见下文)

> dt
             idate    itime windgeschwindigkeit windgeschwindigkeit_1
     1: 1958-02-01 00:00:00          -0.9049475            -0.9049475
     2: 1958-02-01 01:00:00          -0.9049475            -0.9049475
     3: 1958-02-01 02:00:00          -0.9049475            -1.0049475
     4: 1958-02-01 03:00:00          -1.0049475            -2.0049475
     5: 1958-02-01 04:00:00          -2.0049475            -2.0049475
    ---                                                              
498020: 2014-11-24 19:00:00          -1.0852256            -0.7852256
498021: 2014-11-24 20:00:00          -0.7852256            -0.8852256
498022: 2014-11-24 21:00:00          -0.8852256            -1.0852256
498023: 2014-11-24 22:00:00          -1.0852256            -1.3852256
498024: 2014-11-24 23:00:00          -1.3852256                    NA

但是,当NA增加时,所有24的倍数都是list(idate,itime+lagg*3600) 0:23 1:24 itime 小时,数据表无法与itime小时匹配 24 任何结果。

> dt[c(24,48)]
        idate    itime windgeschwindigkeit windgeschwindigkeit_1
1: 1958-02-01 23:00:00           0.5950525                    NA
2: 1958-02-02 23:00:00           4.0939842                    NA

任何想法如何解决这个问题,例如将idate和itime增加1小时? 非常感谢任何帮助。

我成功完成了以下工作"解决方案"使用as.POSIXct,但效率不高:

setkeyv(dt, c("idate","itime"))
m_col = "windgeschwindigkeit"
pm_col = parse(text="windgeschwindigkeit")
lagg = 1
new_time <- dt[,IDateTime(as.POSIXct(idate)+itime+lagg*3600)]
dt[, paste0(m_col,"_",lagg) :=
     dt[new_time, eval(pm_col), roll=-1]]

我的数据头部的输入:

structure(list(idate = structure(c(-4352L, -4352L, -4352L, -4352L, 
-4352L, -4352L, -4352L, -4352L, -4352L, -4352L, -4352L, -4352L, 
-4352L, -4352L, -4352L, -4352L, -4352L, -4352L, -4352L, -4352L, 
-4352L, -4352L, -4352L, -4352L, -4351L, -4351L, -4351L, -4351L, 
-4351L, -4351L, -4351L, -4351L, -4351L, -4351L, -4351L, -4351L, 
-4351L, -4351L, -4351L, -4351L, -4351L, -4351L, -4351L, -4351L, 
-4351L, -4351L, -4351L, -4351L), class = c("IDate", "Date")), 
    itime = structure(c(0L, 3600L, 7200L, 10800L, 14400L, 18000L, 
    21600L, 25200L, 28800L, 32400L, 36000L, 39600L, 43200L, 46800L, 
    50400L, 54000L, 57600L, 61200L, 64800L, 68400L, 72000L, 75600L, 
    79200L, 82800L, 0L, 3600L, 7200L, 10800L, 14400L, 18000L, 
    21600L, 25200L, 28800L, 32400L, 36000L, 39600L, 43200L, 46800L, 
    50400L, 54000L, 57600L, 61200L, 64800L, 68400L, 72000L, 75600L, 
    79200L, 82800L), class = "ITime"), windgeschwindigkeit = c(-0.904947510665982, 
    -0.904947510665982, -0.904947510665982, -1.00494751066598, 
    -2.00494751066598, -2.00494751066598, -2.90494751066598, 
    -2.50494751066598, -2.50494751066598, -1.40494751066598, 
    -1.50494751066598, -1.30494751066598, -1.00494751066598, 
    -0.704947510665983, -0.504947510665983, -0.504947510665983, 
    -0.204947510665982, -0.104947510665983, 0.0950524893340177, 
    1.09505248933402, 0.195052489334017, -0.204947510665982, 
    0.0950524893340177, 0.595052489334018, 1.79398421777773, 
    2.99398421777773, 3.39398421777773, 3.29398421777773, 2.99398421777773, 
    2.89398421777773, 1.89398421777773, 0.593984217777727, 0.293984217777727, 
    -0.706015782222273, -0.706015782222273, -0.806015782222273, 
    -0.406015782222273, 0.893984217777727, -0.206015782222273, 
    -0.606015782222273, -0.00601578222227328, 0.693984217777727, 
    1.29398421777773, 2.49398421777773, 3.79398421777773, 4.29398421777773, 
    3.99398421777773, 4.09398421777773)), .Names = c("idate", 
"itime", "windgeschwindigkeit"), row.names = c(NA, -48L), class = c("data.table", 
"data.frame"), sorted = c("idate", "itime")) 

1 个答案:

答案 0 :(得分:4)

我只是推动了能够生成多个周期的超前/滞后向量的函数shift()。它总是返回一个列表。见this issue。虽然要使用它,但您需要v1.9.5,这是当前的开发版本 - 安装说明here

有了这个,IIUC,你想做的事情可以完成如下:

require(data.table) ## v1.9.5+
dt[, lead_1 := shift(windgeschwindigkeit, 1L, type="lead"), by=.(idate)]

这假设与itime对应的idate列都是正确的顺序。如果没有,你可以这样做:

dt[order(idate, itime), lead_1 := shift(windgeschwindigkeit, 1L, type="lead"), by=.(idate)]