按组检查是否基于其他列存在上一个条目

时间:2019-02-04 11:03:08

标签: r data.table

如果要在第三个日期列中输入相同的ID的上一年条目,我想在data.table中创建一个与另一个ID列相同的列。

我效率极低的解决方案:

library(data.table)
set.seed(123)
DT = data.table(
  ID = c("b","b","b","a","a","c"),
  dates = sample(seq(as.Date('2016/01/01'), as.Date('2019/01/01'), by="day"), 12)
)

setorder(DT, ID, dates)
DT[, Desired_Column:=DT[ID == .BY[[1]] & year(dates) < year(.BY[[2]]), ID[.N]], by=.(ID, dates)]

我的问题: 为什么在大型数据集上速度这么慢,又有什么方法可以更快呢?

编辑:初始版本无法解决整个问题。令我惊讶的是,过滤器year( dates ) > min( year( dates ) )按组工作,但实际上不起作用。我更改了dates列,以便可以输入年份2016。现在,a组没有早于2017的条目,该条目应该成为Desired_Column NA的第一条条目。

这是我想要的输出:

      ID      dates Desired_Column
 1:  a 2017-05-11           <NA>
 2:  a 2018-08-24              a
 3:  a 2018-10-24              a
 4:  a 2018-11-06              a
 5:  b 2016-11-11           <NA>
 6:  b 2017-03-23              b
 7:  b 2017-07-30              b
 8:  b 2017-08-23              b
 9:  b 2018-05-13              b
10:  b 2018-08-30              b
11:  c 2016-02-19           <NA>
12:  c 2017-05-07              c

3 个答案:

答案 0 :(得分:4)

我的方法

DT[ DT[, .I[ year(dates) > min(year(dates))], by = "ID"]$V1, Desired_Column := ID][]

#     ID      dates Desired_Column
#  1:  a 2017-05-11           <NA>
#  2:  a 2018-08-24              a
#  3:  a 2018-10-24              a
#  4:  a 2018-11-06              a
#  5:  b 2016-11-11           <NA>
#  6:  b 2017-03-23              b
#  7:  b 2017-07-30              b
#  8:  b 2017-08-23              b
#  9:  b 2018-05-13              b
# 10:  b 2018-08-30              b
# 11:  c 2016-02-19           <NA>
# 12:  c 2017-05-07              c

基准化

microbenchmark::microbenchmark( 
  my_solution = DT[ DT[, .I[ year( dates ) > min( year( dates ) ) ], by = "ID"]$V1, Desired_Column := ID][],
  your_solution = DT[, Desired_Column:=DT[ID == .BY[[1]] & year(dates) < year(.BY[[2]]), ID[.N]], by=.(ID, dates)][],
  akrun = {
    DT[, yr := year(dates)]
    DT[DT[, .(yr = first(yr)), ID],  Desired_Column := ID, on = .(ID, yr > yr)]
  }
)

# Unit: milliseconds
#          expr      min       lq     mean   median       uq       max neval
#   my_solution 1.349660 1.470769 1.670500 1.612211 1.836653  2.764091   100
# your_solution 4.317707 4.510213 4.877906 4.656327 4.893572 21.164655   100
#         akrun 3.637755 3.812187 4.320189 4.197804 4.675306 10.018512   100

以及长度为1,000的数据集

# Unit: milliseconds
#          expr        min         lq       mean     median         uq       max neval
#   my_solution   1.635860   1.787998   2.323437   2.038197   2.504854  10.82108   100
# your_solution 342.582218 352.706475 367.424500 359.987257 375.076633 467.85023   100
#         akrun   3.749825   4.291949   5.448715   4.949456   5.368815  39.72218   100

以及长度为1,000,000的数据集

# Unit: milliseconds
#          expr      min       lq     mean   median       uq      max neval
#   my_solution 270.8044 280.4150 324.1195 284.5502 390.1511 393.2282    10
# your_solution   - I did not dare to run ;-)
#         akrun 166.2049 167.8109 209.5945 178.2247 291.4220 297.0243    10

结论

我的subsetting-answer最有效地处理data.tables(最多约50,000行),超过该大小,@ akrun的非等值联接解决方案是性能的赢家。

答案 1 :(得分:3)

这里是非等额联接的选项。由于已对“日期”列进行了排序,因此可以对按“ ID”分组的{​​{1}}“年”进行子集化,并在非等价自连接中使用它来创建“ Desired_Column”,从而避免了获取first最高价值

min

答案 2 :(得分:1)

这是一种方法

library(data.table)
library(lubridate)
DT[year(dates)>(min(year(dates))), Desired_Column:=ID, by=.(ID, year(dates))]