【问题标题】:How can I keep track of total transaction amount sent from an account each last 6 month?如何跟踪过去 6 个月从账户发送的总交易金额?
【发布时间】:2020-12-20 15:48:38
【问题描述】:

这是我的交易数据

data 

id          from    to          date        amount  
<int>       <fctr>  <fctr>      <date>      <dbl>
19521       6644    6934        2005-01-01  700.0
19524       6753    8456        2005-01-01  600.0
19523       9242    9333        2005-01-01  1000.0
…           …       …           …           …
1055597     9866    9736        2010-12-31  278.9
1053519     9868    8644        2010-12-31  242.8
1052790     9869    8399        2010-12-31  372.2

现在,对于from 列中的每个不同帐户,我想跟踪他们在进行交易时过去 6 个月发送的交易金额,因此我想根据交易日期在进行了特定交易。

为了看得更清楚,这里我只考虑帐户5370。那么,让我们考虑以下数据:

id          from    to          date        amount  
<int>       <fctr>  <fctr>      <date>      <dbl>
18529       5370    9356        2005-05-31  24.4
13742       5370    5605        2005-08-05  7618.0
9913        5370    8567        2005-09-12  21971.0
2557        5370    5636        2005-11-12  2921.0
18669       5370    8933        2005-11-30  169.2
35900       5370    8483        2006-01-31  71.5
51341       5370    7626        2006-04-11  4214.0
83324       5370    9676        2006-08-31  261.1
100277      5370    9105        2006-10-31  182.0
103444      5370    9772        2006-11-08  16927.0

5370 进行的第一笔交易是在 2005-05-31 上进行的。所以在此之前没有任何记录。这就是为什么这是 5370 的开始日期点(因此,每个不同的帐户将根据他们进行首次交易的日期有自己的开始日期点)。因此,当时5370 在过去 6 个月发送的总交易量仅为 24.4。转到5370 的下一个事务,是在2005-08-05 上进行的第二个事务。当时5370在过去6个月发送的总交易金额为24.4 + 7618.0 = 7642.4。所以,输出应该如下:

id          from    to          date        amount     total_trx_amount_sent_in_last_6month_by_from
<int>       <fctr>  <fctr>      <date>      <dbl>      <dbl>
18529       5370    9356        2005-05-31  24.4       24.4 
13742       5370    5605        2005-08-05  7618.0     (24.4+7618.0)=7642.4
9913        5370    8567        2005-09-12  21971.0    (24.4+7618.0+21971.0)=29613.4
2557        5370    5636        2005-11-12  2921.0     (24.4+7618.0+21971.0+2921.0)=32534.4
18669       5370    8933        2005-11-30  169.2      (7618.0+21971.0+2921.0+169.2)=32679.2
35900       5370    8483        2006-01-31  71.5       (7618.0+21971.0+2921.0+169.2+71.5)=32750.7
51341       5370    7626        2006-04-11  4214.0     (2921.0+169.2+71.5+4214.0)=7375.7
83324       5370    9676        2006-08-31  261.1      (4214.0+261.1)=4475.1
100277      5370    9105        2006-10-31  182.0      (261.1+182.0)=443.1
103444      5370    9772        2006-11-08  16927.0    (261.1+182.0+16927.0)=17370.1

为了计算,我从每行的交易日期中减去了 180 天(约 6 个月)。这就是我选择应该汇总的金额的方式。

那么,考虑到所有不同的帐户,我如何才能为整个数据实现这一目标?

PS:我的数据有 100 万行,因此该解决方案也应该在大型数据集上运行得更快。

【问题讨论】:

    标签: r date transactions aggregate-functions aggregation


    【解决方案1】:

    使用dplyr 的方法可能是:

    library(dplyr)
    df %>%
      group_by(from) %>%
      mutate(total_trx = purrr::map_dbl(date, 
                         ~sum(amount[between(date, .x - 180, .x)])))
    
    #      id  from    to date        amount total_trx
    #    <int> <int> <int> <date>       <dbl>     <dbl>
    # 1  18529  5370  9356 2005-05-31    24.4      24.4
    # 2  13742  5370  5605 2005-08-05  7618      7642. 
    # 3   9913  5370  8567 2005-09-12 21971     29613. 
    # 4   2557  5370  5636 2005-11-12  2921     32534. 
    # 5  18669  5370  8933 2005-11-30   169.    32679. 
    # 6  35900  5370  8483 2006-01-31    71.5   32751. 
    # 7  51341  5370  7626 2006-04-11  4214      7376. 
    # 8  83324  5370  9676 2006-08-31   261.     4475. 
    # 9 100277  5370  9105 2006-10-31   182       443. 
    #10 103444  5370  9772 2006-11-08 16927     17370. 
    

    如果您的数据量很大,您可以在data.table 中使用上述方法,这可能是有效的。

    library(data.table)
    setDT(df)[, total_trx := sapply(date, function(x) 
                             sum(amount[between(date, x - 180, x)])), from]
    

    【讨论】:

    • 谢谢!第一个也很好用。它很有帮助。
    • 第一个更快,但它会产生一些警告,你能说出原因吗?
    • 警告是关于由小标题引起的“未知或未初始化的列”,但输出是正确的。
    • 我认为您可以忽略这些警告。也许重新启动 R Studio 会解决它。
    • 你能看看我的编辑吗,我对这个话题还有一个问题
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多