鉴于此数据:
DATE_ID FRACTION
20160922 0
20160929 0
20161006 0
20161013 0
20161020 0
20161027 0.07
20161103 0.22
20161110 0.5
20161117 0.8
20161124 0.95
20161201 1
20161208 1
20161215 1
20170105 1
20170112 1
20170119 1
根据定义,重要范围是最大DATE_ID与最小FRACTION之间的间隔,最小DATE_ID和最大分数:
20161020 0
20161027 0.07
20161103 0.22
20161110 0.5
20161117 0.8
20161124 0.95
20161201 1
我想只使用data.table syntax / internals来做这件事。 已经尝试过这样的事情:
# Retrieve the upper bound of the interval
t[t[FRACTION == 1, .I[which.min(DATE_ID)]]]
但它返回相对索引。我错过了一些明显的东西。
答案 0 :(得分:1)
您可以像这样获得上限和下限:
library(data.table)
DT <- fread("DATE_ID FRACTION
20160922 0
20160929 0
20161006 0
20161013 0
20161020 0
20161027 0.07
20161103 0.22
20161110 0.5
20161117 0.8
20161124 0.95
20161201 1
20161208 1
20161215 1
20170105 1
20170112 1
20170119 1")
# Working with date format can be easier
DT[, DATE_ID := anytime::anydate(DATE_ID)]
# Calculate the bound
lower <- DT[FRACTION == min(FRACTION), max(DATE_ID)]
upper <- DT[FRACTION == max(FRACTION), min(DATE_ID)]
# Filter the table
DT[DATE_ID %between% c(lower, upper)]
#> DATE_ID FRACTION
#> 1: 2016-10-20 0.00
#> 2: 2016-10-27 0.07
#> 3: 2016-11-03 0.22
#> 4: 2016-11-10 0.50
#> 5: 2016-11-17 0.80
#> 6: 2016-11-24 0.95
#> 7: 2016-12-01 1.00