我应该如何使用R代码计算余额

时间:2016-03-05 12:13:54

标签: r

   ID   Type    Type_Description    Amount  Balance
41  85  incoming transaction        100.0    100.0
41  55  outgoing transaction         76.6     23.4
41  55  outgoing transaction         23.4      0
41  90  incoming transaction         24.1     24.1
41  55  outgoing transaction         14.19     9.91
41  63  Sweep Off Amount              9.91     0
42  85  incoming transaction        100.0     100
42  55  outgoing transaction         76.6     23.4
42  55  outgoing transaction         23.4      0
42  90  incoming transaction         24.1     24.1
42  55  outgoing transaction         14.19    9.91
42  63  Sweep Off Amount              9.91    0

1 个答案:

答案 0 :(得分:0)

新答案:您可以执行相同的操作组,并考虑不同类型的交易,如下所示:

df1 <- transform(df1, Amount = Amount*c(-1,1,0)[(Type %in% c(85,90)) + 1L])
df1 <- transform(df1, Balance = ave(df1$Amount, df1$ID, FUN = function(x) round(cumsum(x),2)))

或者:

library(dlyr)
df1 %>%
  group_by(ID) %>%
  mutate(Amount = Amount*c(-1,1)[(Type %in% c(85,90)) + 1L],
         Balance = round(cumsum(Amount),2))

给出:

Source: local data frame [12 x 5]
Groups: ID [2]

      ID  Type     Type_Description Amount Balance
   (int) (int)               (fctr)  (dbl)   (dbl)
1     41    85 incoming_transaction 100.00  100.00
2     41    55 outgoing_transaction -76.60   23.40
3     41    55 outgoing_transaction -23.40    0.00
4     41    90 incoming_transaction  24.10   24.10
5     41    55 outgoing_transaction -14.19    9.91
6     41    63     Sweep_Off_Amount  -9.91    0.00
7     42    85 incoming_transaction 100.00  100.00
8     42    55 outgoing_transaction -76.60   23.40
9     42    55 outgoing_transaction -23.40    0.00
10    42    90 incoming_transaction  24.10   24.10
11    42    55 outgoing_transaction -14.19    9.91
12    42    63     Sweep_Off_Amount  -9.91    0.00

使用 data.table ,您可以:

library(data.table)
setDT(df1)[, Amount := Amount*c(-1,1)[(Type %in% c(85,90)) + 1L]
           ][, Balance := round(cumsum(Amount),2), by = ID][]

旧回答:如果我理解正确,您希望从Balance计算Trs_Amount,其中Transaction_Type==85表示传入的交易,Transaction_Type==55传出的交易。为此,您可以按如下方式使用条件cumsum:

mydf <- transform(mydf, Balance = round(cumsum(ifelse(Transaction_Type==85,
                                                      Trs_Amount, 
                                                      -1*Trs_Amount)),2))

给出:

> mydf
   ID Transaction_Type Trs_Amount Balance
1 121               85     100.00  100.00
2 121               55      21.52   78.48
3 121               55      36.01   42.47
4 121               55      15.57   26.90
5 121               55       2.02   24.88
6 121               55      23.49    1.39
7 121               55       1.39    0.00

使用 dplyr 包,你可以这样做:

library(dplyr)
mydf %>%
  mutate(Balance = round(cumsum(ifelse(Transaction_Type==85,
                                       Trs_Amount, 
                                       -1*Trs_Amount)),
                         2))

给出了相同的结果。一个稍微替代的实现是:

mydf %>%
  mutate(Trs_Amount = Trs_Amount*c(-1,1)[(Transaction_Type==85) + 1L],
         Balance = round(cumsum(Trs_Amount),2))

给出:

   ID Transaction_Type Trs_Amount Balance
1 121               85     100.00  100.00
2 121               55     -21.52   78.48
3 121               55     -36.01   42.47
4 121               55     -15.57   26.90
5 121               55      -2.02   24.88
6 121               55     -23.49    1.39
7 121               55      -1.39    0.00