【问题标题】:Dynamic and conditional inserting of new rows that come after a certain Date动态和有条件地插入某个日期之后的新行
【发布时间】:2016-11-05 18:34:46
【问题描述】:

以下是我的数据框的样子(感谢经验丰富的社区的精彩编辑):

library(data.table)
df <- fread('Account       Date     Blue     Red   Amount  
            A          1/1/2016      1        0     100    
            A          2/1/2016      1        1     200    
            B          1/10/2016     0        1     300    
            B          2/10/2016     1        1     400')
df[, Date := as.Date(Date, format="%m/%d/%Y")]

blue <- fread('Date      Amount  
              6/1/2015    55     
              1/31/2016   55     
              2/28/2016   65     
              3/31/2016   75')
blue[, Date := as.Date(Date, format="%m/%d/%Y")]

red <- fread('Date      Amount  
             12/31/2015  43     
             1/15/2016   47     
             2/15/2016   67     
             3/15/2016   77')
red[, Date := as.Date(Date, format="%m/%d/%Y")]

在主数据框 df 中,BlueRed 字段描述了 Account 属于哪个类别给定时间点。例如,截至 2016 年 1 月 1 日,帐户 A 仅属于 蓝色 类别。 bluered 数据框描述了向蓝色和红色类别中的所有帐户发放现金的日期。我想在原始 df 中插入新行,只有 df 中的 Date 字段之后的行来自 bluered 数据帧,具体取决于帐户属于 Blue 还是 Red 或两者兼有。

我正在寻找的输出如下所示:

  Account       Date         Blue     Red   Amount  
      A          1/1/2016      1        0     100    
      A          1/31/2016     1        0     55
      A          2/1/2016      1        1     200
      A          2/15/2016     1        1     67 
      A          2/28/2016     1        1     65   
      A          3/15/2016     1        1     77    
      A          3/31/2016     1        1     75   
      B          ..............................

在输出中,截至 2016 年 1 月 1 日,帐户 A 仅属于 蓝色 类别。我的目标是在 blue 表中立即 AFTER 1/1/2016 找到日期,即 2016 年 1 月 31 日 strong> 然后插入。我不想从 red 表中插入 1/15/2016,因为截至 1 月 1 日,帐户 A 不是 Red 类别2016 年。我对插入字段显示 NA 的蓝色和红色字段表示满意。

我的想法是尝试rbind(df, blue, red), by="Account"),但不知道如何结合仅根据帐户在给定时间点所属的类别插入较晚日期的条件。

【问题讨论】:

  • 所需的输出不一致恕我直言:对于df 的第一行,您只选择与条件匹配的第一行,而对于第二行,您包含多个。下面是一个解决方案,它从bluered 的匹配行中选择第一行。
  • @ProcrastinatusMaximus 从第一行(2016 年 1 月 1 日)开始,A 只是蓝色类别,这就是为什么我从介于 1/1 和 2/1 之间的蓝色表中放入 1/31 .截至 df(2/1/2016) 的第二行,A 既是蓝色又是红色。这就是为什么我从蓝色和红色表格中带来所有后续日期。

标签: r data.table dplyr zoo


【解决方案1】:

一种可能的方法:

# combine the 'blue' & 'red' into one and create an 'colcat' column on the fly
br <- rbindlist(list(blue, red), 
                idcol = 'colcat')[, colcat := c('blue','red')[colcat]]

# loop over the rows of 'df', select the needed rows from 'bluered' 
# and punt the result into a list
brlist <- lapply(df$Date, function(x) br[Date > x][order(Date)])

# loop over the rows, select the needed rows from 'bluered' & bind them together
lst <- lapply(1:nrow(df), function(i) {
  idx <- c('blue','red')[c(c(1)[!!df[i][['Blue']]], c(2)[!!df[i][['Red']]])]
  incs <- brlist[[i]][colcat %in% idx][, .SD[1], colcat][, .(Account = df$Account[i], Date, Blue = df$Blue[i], Red = df$Red[i], Amount)]
  rbind(df[i],incs)
})

# bind the resulting list into one 'data.table' again
DT <- rbindlist(lst)

给出:

> DT
    Account       Date Blue Red Amount
 1:       A 2016-01-01    1   0    100
 2:       A 2016-01-31    1   0     55
 3:       A 2016-02-01    1   1    200
 4:       A 2016-02-15    1   1     67
 5:       A 2016-02-28    1   1     65
 6:       B 2016-01-10    0   1    300
 7:       B 2016-01-15    0   1     47
 8:       B 2016-02-10    1   1    400
 9:       B 2016-02-15    1   1     67
10:       B 2016-02-28    1   1     65

【讨论】:

  • 感谢您的回答!为什么 2016 年 3 月 15 日和 2016 年 3 月 31 日没有出现在输出中?它在组合的“br”表中。
猜你喜欢
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2012-12-07
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多