r-我如何跟踪最近6个月从一个账户发送的总交易金额



这是我的交易数据

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万行,所以解决方案在大型数据集上也应该运行得更快。

使用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]

相关内容

最新更新