使用以前的值按组填写缺少日期

时间:2017-08-09 20:10:35

标签: r data.table

我们说我有以下data.table

library(data.table)
set.seed(123)
df <- as.data.table(data.frame(date = c("2017-01-01", "2017-01-05", "2017-01-08", "2017-01-01", "2017-01-05", "2017-01-08"), 
                 value = rnorm(6),
                 mygroup = rep(LETTERS[1:2], each = 3)))

我想用最后一个&#39;填写错过的日期。按组的价值。我找到的最接近的是this question,它显示了如何在没有分组的情况下进行此操作。

all_dates <- seq(from = as.Date("2017-01-01"), 
                   to = as.Date("2017-01-08"), 
                   by = "days")

df[J(all_dates), roll=Inf]

但是,我需要按组执行此操作并使用by结果出错

  

[.data.table中的错误(df,J(all_dates),roll = Inf,by = mygroup):     &#39;由&#39;或者&#39; keyby&#39;提供但不是j

1 个答案:

答案 0 :(得分:3)

我们可以在滚动连接中添加mygroup作为另一列:

df[, date := as.Date(date)]

df[
  df[, .(date = seq(first(date), last(date), by="day")), by=mygroup], 
  on=.(mygroup, date), 
  roll=TRUE]

          date       value mygroup
 1: 2017-01-01 -0.56047565       A
 2: 2017-01-02 -0.56047565       A
 3: 2017-01-03 -0.56047565       A
 4: 2017-01-04 -0.56047565       A
 5: 2017-01-05 -0.23017749       A
 6: 2017-01-06 -0.23017749       A
 7: 2017-01-07 -0.23017749       A
 8: 2017-01-08  1.55870831       A
 9: 2017-01-01  0.07050839       B
10: 2017-01-02  0.07050839       B
11: 2017-01-03  0.07050839       B
12: 2017-01-04  0.07050839       B
13: 2017-01-05  0.12928774       B
14: 2017-01-06  0.12928774       B
15: 2017-01-07  0.12928774       B
16: 2017-01-08  1.71506499       B

&#34;滚动&#34;始终发生在on=的最后一列。

如果表格中有更多列,我们只想填写其中的一些......

# extend example
set.seed(1)
df[, y := rpois(.N, 1)]

# build new table
newDT = df[, .(date = seq(first(date), last(date), by="day")), by=mygroup]

roll_cols = "value"
newDT[, (roll_cols) := 
  df[newDT, on=.(mygroup, date), roll=TRUE, mget(paste0("x.", roll_cols))]]

noroll_cols = "y"
newDT[df, on=.(mygroup, date), (noroll_cols) := mget(paste0("i.", noroll_cols)) ]

    mygroup       date       value  y
 1:       A 2017-01-01 -0.56047565  0
 2:       A 2017-01-02 -0.56047565 NA
 3:       A 2017-01-03 -0.56047565 NA
 4:       A 2017-01-04 -0.56047565 NA
 5:       A 2017-01-05 -0.23017749  1
 6:       A 2017-01-06 -0.23017749 NA
 7:       A 2017-01-07 -0.23017749 NA
 8:       A 2017-01-08  1.55870831  1
 9:       B 2017-01-01  0.07050839  2
10:       B 2017-01-02  0.07050839 NA
11:       B 2017-01-03  0.07050839 NA
12:       B 2017-01-04  0.07050839 NA
13:       B 2017-01-05  0.12928774  0
14:       B 2017-01-06  0.12928774 NA
15:       B 2017-01-07  0.12928774 NA
16:       B 2017-01-08  1.71506499  2