2015-03-10 16 views
6

Dato il seguente data.table con dati finanziari:Dati Finanziari - R data.table - gruppo da condiction

userId systemBankId accountId valueDate quantity description 
871  0065   6422  2013-02-28 -52400  AMORTIZACION PRESTAMO  
871  0065   6422  2013-03-28 -52400 AMORTIZACION PRESTAMO 
871  0065   6422  2013-04-01 -3000000 AMORTIZACION PRESTAMO 
871  0065   6422  2013-04-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-05-31 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-06-28 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-07-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-08-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-09-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-10-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-11-29 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2013-12-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-01-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-02-28 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-03-31 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-04-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-05-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-06-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-07-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-08-29 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-09-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-10-30 -52349 AMORTIZACION PRESTAMO 
871  0065   6422  2014-11-28 -52349 AMORTIZACION PRESTAMO 

voglio gruppo da userId, systemBankId, accountId, e quantity:

dt[userId==871L,.N,by=.(userId,systemBankId,accountId,quantity)] 

Il risultato è il seguente:

userId systemBankId accountId quantity N 
    871   0065  6422 -52400  3 
    871   0065  6422 -3000000  1 
    871   0065  6422 -52349 20 

Ma il primo e il terzo sono la stessa transazione: il pagamento del mutuo e il secondo è un prestito.

Voglio gruppo dal nel modo seguente:

userId systemBankId accountId quantity N 
    871   0065  6422 -XXXXX 23 
    871   0065  6422 -3000000 1 

Così si può vedere che in 24 mesi questo utente ha 23 operazioni di mutuo e 1 pagamento delle transazioni di prestito.

La domanda è: c'è un modo semplice per farlo? (i.e):

dt[userId==871L,.N,by=.(userId,systemBankId,accountId,(quantity %between% c(-quantity*0.20,quantity*0,20))] 

Per i pagamenti compresi nell'intervallo [-20%, 20%] sono considerati uguali.

Grazie in anticipo.

Cordiali saluti.


Per ottenere il frame di dati dei dati di cui sopra:

structure(list(userId = c(871L, 871L, 871L, 871L, 871L, 871L, 
871L, 871L, 871L, 871L, 871L, 871L, 871L, 871L, 871L, 871L, 871L, 
871L, 871L, 871L, 871L, 871L, 871L), systemBankId = c(65L, 65L, 
65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 
65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L), accountId = c(6422L, 
6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 
6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 6422L, 
6422L, 6422L, 6422L, 6422L), valueDate = structure(c(2L, 4L, 
1L, 10L, 23L, 5L, 14L, 16L, 17L, 19L, 8L, 21L, 9L, 3L, 22L, 11L, 
12L, 13L, 15L, 7L, 18L, 20L, 6L), .Label = c("01/04/2013", "28/02/2013", 
"28/02/2014", "28/03/2013", "28/06/2013", "28/11/2014", "29/08/2014", 
"29/11/2013", "30/01/2014", "30/04/2013", "30/04/2014", "30/05/2014", 
"30/06/2014", "30/07/2013", "30/07/2014", "30/08/2013", "30/09/2013", 
"30/09/2014", "30/10/2013", "30/10/2014", "30/12/2013", "31/03/2014", 
"31/05/2013"), class = "factor"), quantity = c(-52400L, -52400L, 
-3000000L, -52349L, -52349L, -52349L, -52349L, -52349L, -52349L, 
-52349L, -52349L, -52349L, -52349L, -52349L, -52349L, -52349L, 
-52349L, -52349L, -52349L, -52349L, -52349L, -52349L, -52349L 
), description = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "AMORTIZACION PRESTAMO", class = "factor")), .Names = c("userId", 
"systemBankId", "accountId", "valueDate", "quantity", "description" 
), class = "data.frame", row.names = c(NA, -23L)) 

UPDATE

Il passo finale è quello di segnare nel dataset originale le transazioni che sono il pagamento dei mutui e che sono pagamenti di prestiti.

Sulla base delle risposte che faccio:

a) criterio: in un periodo di tempo di 24 mesi ,, se ci sono venti o più operazioni ricorrenti di userId, systemBankId, accountid, la quantità (-20%, 20%) sono pagamenti di ipoteche:

tmp <- dt[userId==871L,.N,by=.(userId,systemBankId,accountId,round(quantity * 5, -floor(log10(abs(quantity))))/5)][N>20,list(userId,systemBankId,accountId,round,N)] 

userId systemBankId accountId round N 
871   0065  6422 -52000 23 

So che ci sono 23 transazioni ipotecarie.

b) ho bisogno di identificare questo 23 transazioni:

tmp2 <- dt[userId==871L,list(userId,systemBankId,accountId,round=round(quantity * 5, -floor(log10(abs(quantity))))/5)] 

merge(tmp,tmp2,by=c('userId','systemBankId','accountId','round')) 

    userId systemBankId accountId round N 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
    871   0065  6422 -52000 23 
userId systemBankId accountId round N 

Ok ho identificato le 23 transazioni, ma se ho un transacction con quantità pari a -52000 questo sarà segnato come se fosse un mutuo pagamento anche

La mia domanda è: in base al criterio del pagamento ricorrente Come identifico le transazioni corrette.

Thx in avanzato.

+1

Nel tuo esempio, si hanno solo 2 '-52400' quantità, non 3. –

+5

Qualcosa di simile a' DT [userId == 871L, .N, da =. (userId, systemBankId, accountId, quantity = round (quantità * 5, -floor (log10 (abs (quantità)))/5)] '? – Roland

+1

@Roland dovresti postare questo come risposta –

risposta

4

Forse si dovrebbe occupare in queste righe:

dt[, round.qty := quantity[1] * round(quantity/quantity[1]), by = .(userId, systemBankId, accountId)] 

dt[, .N, by = .(userId, systemBankId, accountId, round.qty)] 
# userId systemBankId accountId round.qty N 
#1: 871   65  6422 -52400 22 
#2: 871   65  6422 -2986800 1 
4

Ecco un trucco veloce con dplyr:

library(dplyr) 
setDF(dt) %>% mutate(quantity = round(quantity/10000, 0)) %>% 
    group_by(userId, systemBankId, accountId, quantity) %>% tally() 

Che dà:

#Source: local data frame [2 x 5] 
#Groups: userId, systemBankId, accountId 
# 
# userId systemBankId accountId quantity n 
#1 871   65  6422  -300 1 
#2 871   65  6422  -5 22 

Modifica

Come citato da David nei commenti, questa risposta è una semplificazione eccessiva. Un approccio più coerente sarebbe qualcosa di simile suggerimento di Roland:

library(dplyr) 
setDF(dt) %>% 
    mutate(quantity = round(quantity * 5, -floor(log10(abs(quantity))))/5) %>% 
    group_by(userId, systemBankId, accountId, quantity) %>% tally() 

o utilizzando data.table:

dt[userId == 871L, .N, by = .(userId, systemBankId, accountId, quantity = round(quantity * 5, -floor(log10(abs(quantity))))/5)] 
+4

Non sei sicuro del perché non è stato sufficiente modificare leggermente i codici OPs a 'dt [userId == 871L, .N, di =. (UserId, systemBankId, accountId, quantity = round (quantità/10000, 0))] ', perché' dplyr'? –

+0

E il fatto che '[-20%, 20%]' sono considerati uguali –

+4

Questa risposta è anche concettualmente sbagliata perché si presume che i pagamenti di morgatge siano in 10.000. Prova 'dt [, quantity: = quantity * 100]' e poi esegui di nuovo il tuo codice e vedi cosa succede. –

0

Ecco una funzione intelligente che è rilevante creato da @dnlbrky qui: Use a value from the previous row in an R data.table calculation

#Create a function to return previous rows 
    rowShift <- function(x, shiftLen = 1L) { 
    r <- (1L + shiftLen):(length(x) + shiftLen) 
    r[r<1] <- NA 
    return(x[r]) 
    } 

Questo ti permetterà di determinare i tuoi intervalli del 20%:

dt$prev_qty_low <-rowShift(dt$quantity,-1) * .8 
dt$prev_qty_high <-rowShift(dt$quantity,-1) * 1.2