我有类似的数据:
我要做的是将第一条记录的stopdate替换为最后一条记录的stopdate,这样我就可以汇总两个间隙列中都有1的所有记录。我知道这是一个f.when语句,但我能想到的一切都不能给我想要的结果。如何确保它只适用于具有此id的记录?
有人能帮忙吗?谢谢!
文本中的示例数据
ID Startdate Stopdate gap_from_previous_in_days gap_to_next_in_days
1 1/1/2021 1/2/2021
1 1/3/2021 1/4/2021 1 1
1 1/5/2021 1/6/2021 1 1
1 1/7/2021 1/8/2021 1 1
1 1/9/2021 1/10/2021 1 1
1 1/11/2021 1/12/2021 1 1
1 1/13/2021 1/14/2021 1 1
1 1/15/2021 1/16/2021 1 1
1 1/17/2021 1/18/2021 1 1
1 1/19/2021 1/20/2021 1 2
我想要的结果:
ID Startdate Stopdate gap_from_previous_in_days gap_to_next_in_days
1 1/1/2021 1/20/2021
所以基本上我要创建一个表,而不是像这样:
ID Startdate Stopdate gap_from_previous_in_days gap_to_next_in_days
1 1/1/2021 1/2/2021
1 1/3/2021 1/4/2021 1 1
1 1/5/2021 1/6/2021 1 1
1 1/7/2021 1/8/2021 1 1
1 1/9/2021 1/10/2021 1 1
1 1/11/2021 1/12/2021 1 1
1 1/13/2021 1/14/2021 1 1
1 1/15/2021 1/16/2021 1 1
1 1/17/2021 1/18/2021 1 1
1 1/19/2021 1/20/2021 1 3
1 1/23/2021 1/25/2021 3
看起来像这样
ID Startdate Stopdate gap_from_previous_in_days gap_to_next_in_days
1 1/1/2021 1/2/2021 3
1 1/23/2021 1/25/2021 3
希望这有助于说明我在做什么。我基本上是想把相隔只有一天的唱片组合起来。
2条答案
按热度按时间yvfmudvl1#
其思想是基于具有
gap_from_previous_in_days != 1
,按该分组列和id分组,获取最早的开始日期和最晚的结束日期,以及它们的相关间隙值:34gzjxbg2#
另一种方法是从给定的间隔构造所有日期。从这个日期列表开始,可以重新计算新的时间间隔。对于这种方法,不使用/不需要间隙列。因此,最后会重新计算,但如果不需要,可以省略。