考虑以下日期:
> dates
[1] "2015-01-03" "2015-01-19" "2015-05-27" "2015-06-12" "2015-06-28" "2015-07-14" "2015-07-30" "2015-08-15" "2015-08-31" "2015-09-16" "2015-10-02" "2015-10-18" "2015-11-19" "2015-12-05" "2016-01-06" "2016-03-10"
[17] "2016-04-11" "2016-05-13" "2016-05-29" "2016-06-14" "2016-07-16" "2016-08-17" "2016-09-18" "2016-10-20" "2016-12-07" "2017-01-08" "2017-03-13" "2017-03-29" "2017-04-14" "2017-06-01" "2017-06-17" "2017-08-04"
[33] "2017-08-20" "2017-09-05" "2017-09-21" "2017-10-07" "2017-12-10" "2017-12-26" "2018-01-27" "2018-02-12" "2018-04-01" "2018-05-03" "2018-06-20" "2018-07-06" "2018-07-22" "2018-08-23" "2018-09-08" "2018-09-24"
[49] "2018-12-13" "2019-01-14" "2019-01-30" "2019-03-19" "2019-04-04" "2019-04-20" "2019-05-06" "2019-05-22" "2019-06-07" "2019-07-09" "2019-07-25" "2019-08-10" "2019-08-26" "2019-09-11" "2019-09-27" "2019-10-13"
[65] "2019-10-29" "2020-01-01" "2020-02-02" "2020-04-22" "2020-06-09" "2020-06-25" "2020-07-11" "2020-08-12" "2020-08-28" "2020-09-13" "2020-09-29" "2020-10-15" "2020-11-16" "2021-02-04" "2021-02-04" "2021-04-25"
[81] "2021-05-11" "2018-12-13" "2018-12-18" "2018-12-23" "2019-01-07" "2019-01-12" "2019-01-17" "2019-01-22" "2019-02-01" "2019-02-26" "2019-03-08" "2019-03-23" "2019-03-28" "2019-04-02" "2019-04-22" "2019-04-27"
[97] "2019-05-07" "2019-05-12" "2019-05-22" "2019-05-27" "2019-06-01" "2019-06-06" "2019-06-11" "2019-06-16" "2019-06-21" "2019-06-26" "2019-07-11" "2019-07-16" "2019-07-21" "2019-07-26" "2019-07-31" "2019-08-10"
[113] "2019-08-15" "2019-08-25" "2019-08-30" "2019-09-04" "2019-09-09" "2019-09-14" "2019-09-19" "2019-09-24" "2019-10-04" "2019-10-14" "2019-10-19" "2019-10-29" "2019-11-03" "2019-11-13" "2019-12-18" "2020-01-27"
[129] "2020-03-12" "2020-03-17" "2020-03-27" "2020-04-21" "2020-04-26" "2020-05-01" "2020-05-06" "2020-05-11" "2020-05-21" "2020-05-26" "2020-05-31" "2020-06-10" "2020-06-15" "2020-06-25" "2020-06-30" "2020-07-05"
[145] "2020-07-10" "2020-07-20" "2020-07-25" "2020-07-30" "2020-08-04" "2020-08-09" "2020-08-14" "2020-08-19" "2020-08-24" "2020-08-29" "2020-09-03" "2020-09-08" "2020-09-13" "2020-09-18" "2020-09-28" "2020-10-03"
[161] "2020-10-13" "2020-10-23" "2020-10-28" "2020-11-02" "2020-11-12" "2020-11-17" "2020-11-27" "2021-01-21" "2021-01-31" "2021-02-25" "2021-03-02" "2021-03-12" "2021-03-17" "2021-03-22" "2021-03-27" "2021-04-01"
[177] "2021-04-06" "2021-04-16" "2021-01-21" "2021-01-31" "2021-02-25" "2021-03-02" "2021-03-12" "2021-03-17" "2021-03-22" "2021-03-27" "2021-04-01" "2021-04-06" "2021-04-16" "2021-04-21" "2021-04-26" "2021-05-01"
[193] "2021-05-06" "2021-05-11"
我如何才能只对间隔至少15天的项目进行子集划分,从上一个项目开始倒计数?
例如,序列中的最后一个日期是2021-05-11
。我会保留这个,但我会跳过2021-05-06
和2021-05-01
,然后保留2021-04-26
。按照相同的模式,下一个要拾取的将是2021-04-06
,并且下一个将是2021-03-27
,依此类推。
我如何以程序化的方式实现这一点?
可再现数据:
dates <- structure(c(16438, 16454, 16582, 16598, 16614, 16630, 16646,
16662, 16678, 16694, 16710, 16726, 16758, 16774, 16806, 16870,
16902, 16934, 16950, 16966, 16998, 17030, 17062, 17094, 17142,
17174, 17238, 17254, 17270, 17318, 17334, 17382, 17398, 17414,
17430, 17446, 17510, 17526, 17558, 17574, 17622, 17654, 17702,
17718, 17734, 17766, 17782, 17798, 17878, 17910, 17926, 17974,
17990, 18006, 18022, 18038, 18054, 18086, 18102, 18118, 18134,
18150, 18166, 18182, 18198, 18262, 18294, 18374, 18422, 18438,
18454, 18486, 18502, 18518, 18534, 18550, 18582, 18662, 18662,
18742, 18758, 17878, 17883, 17888, 17903, 17908, 17913, 17918,
17928, 17953, 17963, 17978, 17983, 17988, 18008, 18013, 18023,
18028, 18038, 18043, 18048, 18053, 18058, 18063, 18068, 18073,
18088, 18093, 18098, 18103, 18108, 18118, 18123, 18133, 18138,
18143, 18148, 18153, 18158, 18163, 18173, 18183, 18188, 18198,
18203, 18213, 18248, 18288, 18333, 18338, 18348, 18373, 18378,
18383, 18388, 18393, 18403, 18408, 18413, 18423, 18428, 18438,
18443, 18448, 18453, 18463, 18468, 18473, 18478, 18483, 18488,
18493, 18498, 18503, 18508, 18513, 18518, 18523, 18533, 18538,
18548, 18558, 18563, 18568, 18578, 18583, 18593, 18648, 18658,
18683, 18688, 18698, 18703, 18708, 18713, 18718, 18723, 18733,
18648, 18658, 18683, 18688, 18698, 18703, 18708, 18713, 18718,
18723, 18733, 18738, 18743, 18748, 18753, 18758), class = "Date")
你可以这样做。。。
#set an indicator vector of length the same as dates, all FALSE except the last one
inds <- rep(FALSE, length(dates))
inds[length(dates)] <- TRUE
curr_date <- tail(dates, 1) #set current date as final date (see loop below)
while(dates[1] <= curr_date-15){ #work through until get to first date
curr_ind <- which(inds)[1] #set current indicator as first TRUE one
curr_date <- dates[curr_ind] #set current date as corresponding date
#find last date at least 15 days before current date
next_ind <- max(which(dates[1:curr_ind] <= curr_date-15))
inds[next_ind] <- TRUE #set corresponding indicator to TRUE
}
dates[inds]
是您想要的日期
tail(dates[inds])
[1] "2021-01-31" "2021-03-02" "2021-03-22" "2021-04-06" "2021-04-26" "2021-05-11"