2015-04-15 12 views
10

Per un esempio dataframe contenente un insieme di coppie longitudinale e latitudinale coordinate ei tempi un oggetto era pure:Elenco tutte le variabili (e le loro proporzioni) in un sottoinsieme di un dataframe

bout <- structure(list(Date = 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, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "02/02/2013", class = "factor"), 
Time = structure(1:30, .Label = c("07:55:40", "07:55:50", 
"07:56:00", "07:56:10", "07:56:20", "07:56:30", "07:56:40", 
"07:56:50", "07:57:00", "07:57:10", "07:57:20", "07:57:30", 
"07:57:40", "07:57:50", "07:58:00", "07:58:10", "07:58:20", 
"07:58:30", "07:58:40", "07:58:50", "07:59:00", "07:59:10", 
"07:59:20", "07:59:30", "07:59:40", "07:59:50", "08:00:00", 
"08:00:10", "08:00:20", "08:00:30"), class = "factor"), Axis1 = c(0L, 
0L, 100L, 500L, 233L, 155L, 60L, 0L, 0L, 115L, 80L, 878L, 
158L, 0L, 13L, 0L, 0L, 25L, 10L, 45L, 33L, 43L, 655L, 498L, 
41L, 151L, 404L, 436L, 28L, 0L), Latitude = c(56.52289678, 
56.52291659, 56.52292762, 56.52295108, 56.52292694, 56.52292513, 
56.5229401, 56.52294825, 56.52295531, 56.52296413, 56.52296976, 
56.52292374, 56.52293053, 56.52292422, 56.52289636, 56.52288866, 
56.52293357, 56.52290114, 56.5228365, 56.52280237, 56.52279844, 
56.52281107, 56.52282589, 56.52279711, 56.52277008, 56.52278785, 
56.52279951, 56.52269176, 56.52270186, 56.52269016), Longitude = c(-2.56573101, 
-2.56578171, -2.56579263, -2.56578099, -2.56575181, -2.56574877, 
-2.56575947, -2.5657653, -2.56577941, -2.56577104, -2.56577004, 
-2.56576048, -2.56575937, -2.56582402, -2.56585538, -2.56579373, 
-2.56572003, -2.56568263, -2.56568237, -2.56570739, -2.56570637, 
-2.56571299, -2.56572322, -2.56566835, -2.56566237, -2.56569353, 
-2.56571833, -2.56563307, -2.56565902, -2.56565666), area = structure(c(1L, 
1L, 2L, 2L, 2L, 2L, 3L, 4L, 5L, 6L, 6L, 7L, 7L, 7L, 8L, 9L, 
10L, 11L, 2L, 2L, 6L, 6L, 6L, 6L, 12L, 13L, 13L, 13L, 13L, 
13L), .Label = c("E456", "E457", "E460", "E461", "E462", 
"E463", "E465", "E468", "E469", "E470", "E471", "E478", "E479" 
), class = "factor"), bout = c(0L, 0L, 1L, 1L, 1L, 
1L, 1L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 0L)), .Names = c("Date", 
"Time", "Axis1", "Latitude", "Longitude", "area", "bout" 
), class = "data.frame", row.names = c(NA, -30L)) 

desidero creare una variabile riassuntiva sull'attività. Finora ho:

bout$Date <- as.Date(bout$Date, origin = "1970-01-01", format = "%d/%m/%Y") 
library(chron) 
bout$Time <- times(as.character(bout$Time)) 

my.stats <- function(x) { 
    min.Date <- min(x$Date) 
    min.Time <- min(x$Time) 
    max.Time <- max(x$Time) 
    time.bout <- max.Time - min.Time 
    return(data.frame(min.Date, min.Time, max.Time, time.bout)) 
} 

library(plyr) 
ddply(bout, .(bout), my.stats) 

Dopo queste variabili, voglio anche un elenco delle zone ogni attività si svolge in

io normalmente uso:.

unique(unlist(bout$area, use.names = FALSE)) 

Ma mi chiedo come riferirei anche la percentuale di tempo trascorso in ciascuna di queste aree? Idealmente vorrei che questo integrato nella funzione di cui sopra. Non ci dovrebbero mai essere più di 10 aree per ogni attività (quindi sono soddisfatto degli spazi vuoti quando sono elencate meno aree).

Ad esempio (mostrando solo due aree):

bout  area.1  time.area.1  area.2  time.area.2 
2  E457  0.80    E460  0.20  
3  E465  0.50    E463  0.33 

Tutte le idee sarebbe molto apprezzato.

risposta

8

Per quello che stai cercando di fare, la matrice sopra con spazi vuoti è uno stile abbastanza goffo e difficile da creare. Quanto segue potrebbe essere più utile (e cattura le stesse informazioni).

Setup dati:

bout$StartTime <- times(as.character(bout$Time)) 
bout$EndTime <- bout$StartTime + times("00:00:10") 

Aggregate

library(data.table) 
bout.result <- setDT(bout)[order(bout), list(Date = min(Date), Dur = max(Time) - min(Time)), by = c("bout","area")] 

Questo vi darà la durata di ogni incontro unico, identificato da attacco e la zona. Ho anche replicato la tua notazione min (data). Se ci sono più di 10 aree/incontro, puoi facilmente aggiungere più righe.

Proporzione:

boutPro <- bout.result[, list(boutDur = times(sum(Dur))), by = "bout"] 
bout.result <- merge(bout.result,boutPro, by = "bout") 
bout.result$prop <- as.numeric(bout.result$Dur/bout.result$boutDur) 

Risultati

bout.result 

    bout area  Date  Dur boutDur  prop 
1: 0 E456 2013-02-02 00:00:20 00:01:20 0.25000000 
2: 0 E461 2013-02-02 00:00:10 00:01:20 0.12500000 
3: 0 E462 2013-02-02 00:00:10 00:01:20 0.12500000 
4: 0 E469 2013-02-02 00:00:10 00:01:20 0.12500000 
5: 0 E470 2013-02-02 00:00:10 00:01:20 0.12500000 
6: 0 E471 2013-02-02 00:00:10 00:01:20 0.12500000 
7: 0 E479 2013-02-02 00:00:10 00:01:20 0.12500000 
8: 1 E457 2013-02-02 00:00:40 00:00:50 0.80000000 
9: 1 E460 2013-02-02 00:00:10 00:00:50 0.20000000 
10: 2 E463 2013-02-02 00:00:20 00:01:00 0.33333333 
11: 2 E465 2013-02-02 00:00:30 00:01:00 0.50000000 
12: 2 E468 2013-02-02 00:00:10 00:01:00 0.16666667 
13: 3 E457 2013-02-02 00:00:20 00:01:50 0.18181818 
14: 3 E463 2013-02-02 00:00:40 00:01:50 0.36363636 
15: 3 E478 2013-02-02 00:00:10 00:01:50 0.09090909 
16: 3 E479 2013-02-02 00:00:40 00:01:50 0.36363636 
+0

Grazie Chris e @Hao - questo è quasi giusto. Tuttavia, come utilizzerei il codice per registrare il fatto che ogni riga di dati rappresenta 10 secondi? (Mi scuso ho trascurato questo quando ho scritto il mio esempio). Quindi, per alcuni periodi in cui è elencata una sola area, il tempo è 0 anziché 10 secondi? Ho provato ad aggiungere questi 10 secondi su: bout.result $ Dur <- bout.result $ Dur + (((10/60)/60)/24) ma questo non sembra funzionare sulle proporzioni (es. E456 è coming out now at 06:07:00 Qualsiasi consiglio sarebbe grato, –

+0

@ KT_1 Forse puoi provare 'bout.result <- setDT (incontro) [ordine (incontro), lista (Date = min (Date), Dur = max (Time) - min (Time) + times ("00:00:10")), da = c ("bout", "area")] '? – Hao

+0

@ KT_1 Giusto per chiarire, ogni" tempo "elencato è un tempo di inizio, che porta a un evento di 10 secondi? In tal caso, vorrei creare una nuova variabile "End Time" definita come "bout $ EndTime <- bout $ Time + times (" 00:00:10 ")'. Quindi, metti 'EndTime' nell'equazione sopra all'interno di' max() 'invece di' Time'. Fai attenzione qui, dato che questo presuppone che tutti gli incontri siano consecutivi (questo era implicito nella tua funzione originale, assicurati che sia quello che vuoi). – Chris

Problemi correlati