Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

Add new variables financial dataset r

$
0
0

I have a question about a financial transaction dataset. The data set look as the following:

    Account_from  Account_to  Value  Timestamp  
1         1           2        10        1
2         1           3        15        1
3         3           4        20        1
4         2           1        10        2
5         1           3        25        2
6         2           1        15        3
7         1           3        10        3
8         1           4        20        4

I would like to create a couple of extra variables based on the account from column. The variables I want to create are : (total value out of account from in the last two timestamps),

(total value incoming of account from in the last two timestamps),

(total transaction value out that timestamp of all transaction done during that timestamp),

(value out of account from / previous value out of account from),

That it will look like this:

    Acc_from  Acc_to  Value  Timestamp  Tot_val_out  Tot_val_inc  Tot_val_out_1time   val_out/prev_val_out
1       1        2     10        1           10           0              45                    0
2       1        3     15        1           25           0              45                   1.5
3       3        4     20        1           20           15             45                    0
4       2        1     10        2           10           10             35                    0
5       1        3     25        2           50           10             35                   1.67
6       2        1     15        3           25           0              25                   1.5
7       1        3     10        3           35           25             25                   0.4
8       1        4     20        4           30           15             20                    2

For example row 5 tot_val_out is 50, this means that account 1 transferred the amount of 50 in the last two timestamps (timestamps 1 and 2). At row 8 account 1 transferred 30 in the last two timestamps (timestamps 3 and 4).

The same should be done for incoming value.

Additionally I would like to create the variables:

(number of transactions done by account from in the previous 4 timestamps)

(number of transactions done by account from in the previous 2 timestamps)

So that:

    Account_from  Account_to  Value  Timestamp  Transactions_previous4  Transactions_previous2
1         1           2        10        1                 1                      1
2         1           3        15        1                 2                      2
3         3           4        20        1                 1                      1
4         2           1        10        2                 1                      1
5         1           3        25        2                 3                      3
6         2           1        15        3                 2                      2
7         1           3        10        3                 4                      2
8         1           4        20        4                 5                      2

At row 8 account 1 has made 5 transactions the last 4 timestamps (timestamps 1 till 4), but in the last 2 timestamps (timestamps 3 and 4) only 2 transactions.

I cannot figure out how to do this. It would be extremely helpfull if someone knows how to do this.

Thanks in advance,


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>