2015-06-29 2 views
11

Desidero contare diverse cose per ID e per ordine (tempo). Ad esempio, con:Tabella dati R - calcolo per ogni riga utilizzando tutte le righe prima della riga corrente

dt = data.table(id=c(1,1,1,2,2,2,3,3,3), hour=c(1,5,5,6,7,8,23,23,23), ip=c(1,1,45,2,2,2,3,1,1), target=c(1,0,0,1,1,1,1,1,0), day=c(1,1,1,1,1,1,3,2,1)) 

    id hour ip target day 
1: 1 1 1  1 1 
2: 1 5 1  0 1 
3: 1 5 45  0 1 
4: 2 6 2  1 1 
5: 2 7 2  1 1 
6: 2 8 2  1 1 
7: 3 23 3  1 3 
8: 3 23 1  1 2 
9: 3 23 1  0 1 

desidero contare, per ogni ID, il numero di giorni attivi, e ore di attivazione, finora, per ogni riga. Il che significa che desiderano ottenere il seguente risultato:

id hour ip target day nb_active_hours_so_far 
1: 1 1 1  1 1 0 (first occurence of id when ordered by hour) 
2: 1 5 1  0 1 1 (has been active in hour "1") 
3: 1 5 45  0 1 2 (has been active in hour "1" and "5") 
4: 2 6 2  1 1 0 (first occurence) 
5: 2 7 2  1 1 1 (has been active in hour "6") 
6: 2 8 2  1 1 2 (has been active in hour "6" and "7") 
7: 3 23 3  1 3 0 (first occurence) 
8: 3 23 1  1 2 1 (has been active in hour "23") 
9: 3 23 1  0 1 1 (has been active in hour "23" only) 

per ottenere il numero totale di ore di attività che vorrei fare:

dt[, nb_active_hours := length(unique(hour)), by=id] 

ma voglio avere la finora parte pure . Non so come farlo ... Qualsiasi aiuto sarebbe apprezzato.

+0

Quindi non si vuole '8' in' id == 2'? –

+2

La parte 'active' non è chiara. – akrun

+0

Scusa se non è chiaro. Desidero contare il numero di valori unici di "ora" visti finora (significato per tutte le righe precedenti, * non usando la riga corrente *). – tucson

risposta

7

Questo è sembra funzionare (anche se havn't testato su diversi casi)

dt[, nb_active_hours_so_far := cumsum(c(0:1, diff(hour[-.N]))>0), by = id] 
# id hour ip target day temp nb_active_hours_so_far 
# 1: 1 1 1  1 1 0      0 
# 2: 1 5 1  0 1 1      1 
# 3: 1 5 45  0 1 1      2 
# 4: 2 6 2  1 1 0      0 
# 5: 2 7 2  1 1 1      1 
# 6: 2 8 2  1 1 2      2 
# 7: 3 23 3  1 3 0      0 
# 8: 3 23 1  1 2 0      1 
# 9: 3 23 1  0 1 0      1 
+1

@akrun sembra promettente, mi chiedo perché non ho pensato a 'rleid'. Dovresti postarlo. –

+1

Grazie, l'ho postato come soluzione separata. @ Frank Ho preso la tua versione 'shift', spero non ti dispiaccia. – akrun

7

Yerk. Ho questa brutta soluzione:

library(data.table) 
dt[ ,nb_active_hours_so_far:=c(0,head(cumsum(c(1,diff(hour)>0)), -1)),id][] 

# id hour ip target day nb_active_hours_so_far 
#1: 1 1 1  1 1      0 
#2: 1 5 1  0 1      1 
#3: 1 5 45  0 1      2 
#4: 2 6 2  1 1      0 
#5: 2 7 2  1 1      1 
#6: 2 8 2  1 1      2 
#7: 3 23 3  1 3      0 
#8: 3 23 1  1 2      1 
#9: 3 23 1  0 1      1 
7

o si potrebbe fare uso delle funzioni rleid/shift dalla versione devel di data.table, vale a dire v1.9.5. Le istruzioni per installare la versione di sviluppo sono here. (Grazie a @Frank per la shift)

library(data.table) 
dt[,nb_active_hours_so_far := shift(rleid(hour),fill=0L), id] 
# id hour ip target day nb_active_hours_so_far 
#1: 1 1 1  1 1      0 
#2: 1 5 1  0 1      1 
#3: 1 5 45  0 1      2 
#4: 2 6 2  1 1      0 
#5: 2 7 2  1 1      1 
#6: 2 8 2  1 1      2 
#7: 3 23 3  1 3      0 
#8: 3 23 1  1 2      1 
#9: 3 23 1  0 1      1 
+0

Mi chiedo se 'shift' sia meglio di un semplice' c' –

+0

@DavidArenburg Immagino che entrambi avrebbero la stessa performance. – akrun