gpt4 book ai didi

r - R 中的日期汇总

转载 作者:行者123 更新时间:2023-12-04 14:25:12 26 4
gpt4 key购买 nike

我有一个如下所示的数据集:

    ID  FromDate    ToDate  SiteID  Cost
1 8/12/2014 8/31/2014 12 245.98
1 9/1/2014 9/7/2014 12 269.35
1 10/10/2014 10/17/2014 12 209.98
1 11/22/2014 11/30/2014 12 309.12
1 12/1/2014 12/11/2014 12 202.14
2 8/16/2014 8/21/2014 12 109.35
2 8/22/2014 8/24/2014 14 44.12
2 9/25/2014 9/29/2014 12 98.75
3 9/15/2014 9/30/2014 23 536.27
3 10/1/2014 10/31/2014 12 529.87
3 11/1/2014 11/30/2014 12 969.55
3 12/1/2014 12/12/2014 12 607.35

我希望它看起来像:
    ID  FromDate    ToDate  SiteID  Cost
1 8/12/2014 9/7/2014 12 515.33
1 10/10/2014 10/17/2014 12 209.98
1 11/22/2014 12/11/2014 12 511.26
2 8/16/2014 8/21/2014 12 109.35
2 8/22/2014 8/24/2014 14 44.12
2 9/25/2014 9/29/2014 12 98.75
3 9/15/2014 9/30/2014 23 536.27
3 10/1/2014 12/12/2014 12 2106.77

如您所见,如果有延续,则日期会累积,并且成本按 ID 和 SiteID 相加。为了帮助某人理解复杂性,如果日期间隔有延续,但 SiteID 发生变化,则它是一个单独的行。如果日期间隔没有延续,则它是一个单独的行。我如何在 R 中做到这一点?此外,我有超过 100,000 个个人 ID。那么最有效的方法/包是什么?

最佳答案

这可能会

df %>% 
mutate(gr = cumsum(FromDate-lag(ToDate, default=1) != 1)) %>%
group_by(gr, ID, SiteID) %>%
summarise(FromDate = min(FromDate),
ToDate = max(ToDate),
cost = sum(Cost))


gr ID SiteID FromDate ToDate cost
(int) (int) (int) (date) (date) (dbl)
1 1 1 12 2014-08-12 2014-09-07 515.33
2 2 1 12 2014-10-10 2014-10-17 209.98
3 3 1 12 2014-11-22 2014-12-11 511.26
4 4 2 12 2014-08-16 2014-08-21 109.35
5 4 2 14 2014-08-22 2014-08-24 44.12
6 5 2 12 2014-09-25 2014-09-29 98.75
7 6 3 23 2014-09-15 2014-09-30 536.27
8 6 3 12 2014-10-01 2014-12-12 2106.77

data.table
library(data.table)
setDT(df)
df[, gr := cumsum(FromDate - shift(ToDate, fill=1) != 1),
][, list(FromDate=min(FromDate), ToDate=max(ToDate), cost=sum(Cost)), by=.(gr, ID, SiteID)]



gr ID SiteID FromDate ToDate cost
1: 1 1 12 2014-08-12 2014-09-07 515.33
2: 2 1 12 2014-10-10 2014-10-17 209.98
3: 3 1 12 2014-11-22 2014-12-11 511.26
4: 4 2 12 2014-08-16 2014-08-21 109.35
5: 4 2 14 2014-08-22 2014-08-24 44.12
6: 5 2 12 2014-09-25 2014-09-29 98.75
7: 6 3 23 2014-09-15 2014-09-30 536.27
8: 6 3 12 2014-10-01 2014-12-12 2106.77

关于r - R 中的日期汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37486572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com