-7
   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
Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

0

New answer: You can do same operation group and take into account for the different type of transactions as follows:

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)))

Or:

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

gives:

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

With data.table you can do:

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

Old answer: If I understand you correctly, you want to calculate the Balance from Trs_Amount where Transaction_Type==85 denotes an incoming transaction and Transaction_Type==55 an outgoing transaction. To achieve that, you could use a conditional cumsum as follows:

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

which gives:

> 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

With the dplyr package you could do:

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

which gives the same result. A slightly alternative implementation would be:

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

which gives:

   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
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Jaap i have updated the data, Now i have to calculate the Balance Please check. – Raju Kommarajula Mar 05 '16 at 18:04
  • @ akrun: I will have to Calclate the Balance by each ID for Each Transaction. – Raju Kommarajula Mar 05 '16 at 18:34
  • for(i in mydf$Trs_Amount ) if(mydf$Transaction_Type==85){ mydf$mydf=mydf$Trs_Amount } else if(mydf$Transaction_Type==55){ mydf$mydf=mydf$Trs_Amount[i]-mydf$Trs_Amount[i-1] } – Raju Kommarajula Mar 05 '16 at 18:38
  • I am Trying the Above code,but I have to add Transaction_Type==90 to the balance by Transactionwise. And Finally i have get new row for each ID(remaining Balance) Sweep Off Amount. – Raju Kommarajula Mar 05 '16 at 18:39
  • @user3872311 will update later – Jaap Mar 05 '16 at 18:57
  • @user3872311 see the update – Jaap Mar 05 '16 at 19:20
  • Thanks jaap, Please see my next question. – Raju Kommarajula Mar 06 '16 at 02:49
  • Please see my next question. – Raju Kommarajula Mar 06 '16 at 05:10
  • @user3872311 You should not be updating your question constantly. If you have a new question, you should post that as a new question (you can refer back to this one off course with a link). Furthermore, I advise you to read the help pages on [ask], how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) and how to [format your question text](http://stackoverflow.com/editing-help). This will make it much more for other people to help you. – Jaap Mar 06 '16 at 08:04
  • OK jaap. Thanks for your Advice, I will do the Same. – Raju Kommarajula Mar 06 '16 at 08:26