2013-01-24 17 views
5

Ho un set di dati che è composto da dati di transazione del cliente. Ha i timestamp di quando accadono determinati eventi. Mi piacerebbe ricevere solo eventi accaduti prima di un determinato evento ma per ogni cliente, non solo un singolo punto nel tempo.R data.table sottotitoli di un sottoinsieme

Ecco un'istantanea:

 custId   date_time_recorded    event   dateTime1 
1 280512544 2012-11-13 15:25:37.947-08   shipped 2012-11-13 15:25:37 
2 280512544 2012-11-13 15:22:42.614-08  statusCheck 2012-11-13 15:22:42 
3 280512544 2012-11-13 15:03:16.62-08  statusCheck 2012-11-13 15:03:16 
4 280512544 2012-11-13 15:01:35.149-08  statusCheck 2012-11-13 15:01:35 
5 280512544 2012-11-13 14:45:41.964-08  status-picked 2012-11-13 14:45:41 
6 280512544 2012-11-13 14:44:57.664-08 warehouse_notified 2012-11-13 14:44:57 
7 280512544 2012-11-13 14:44:57.644-08  statusCheck 2012-11-13 14:44:57 
8 280512544 2012-11-13 13:05:15.725-08  recordCreated 2012-11-13 13:05:15 
9 280510610 2012-11-13 09:22:36.427-08   shipped 2012-11-13 09:22:36 
10 280510610 2012-11-13 09:20:07.202-08  statusCheck 2012-11-13 09:20:07 
11 280510610 2012-11-13 09:14:56.182-08  statusCheck 2012-11-13 09:14:56 

Mi piacerebbe ottenere solo gli eventi che si sono verificati prima dell'evento 'spediti'. Attualmente sto usando ddply per realizzare questo, ma ci vuole molto tempo.

keepPreShip <- function(x){ 
shipTime <- fastPOSIXct(x[grep("shipped", x$event, ignore.case = T), "date_time_recorded"],tz = "UTC") 
#shipTime <- fastPOSIXct(x[x$event =="shipped", "date_time_recorded"],tz = "UTC") 
x <- x[x$dateTime1 < shipTime,] 
} 

system.time(eventsMain1 <- ddply(ss1, .(custId), keepPreShip)) 

C'è un modo più veloce per farlo? Forse con data.table?

Ecco un dput dei dati:

> dput(ss1) 
structure(list(custId = c(280512544L, 280512544L, 280512544L, 
280512544L, 280512544L, 280512544L, 280512544L, 280512544L, 280510610L, 
280510610L, 280510610L, 280510610L, 280510610L, 280510610L, 280510610L, 
280510610L, 280511123L, 280511123L, 280511123L, 280511123L), 
    date_time_recorded = c("2012-11-13 15:25:37.947-08", "2012-11-13 15:22:42.614-08", 
    "2012-11-13 15:03:16.62-08", "2012-11-13 15:01:35.149-08", 
    "2012-11-13 14:45:41.964-08", "2012-11-13 14:44:57.664-08", 
    "2012-11-13 14:44:57.644-08", "2012-11-13 13:05:15.725-08", 
    "2012-11-13 09:22:36.427-08", "2012-11-13 09:20:07.202-08", 
    "2012-11-13 09:14:56.182-08", "2012-11-13 09:11:40.438-08", 
    "2012-11-13 09:03:51.571-08", "2012-11-13 09:03:51.461-08", 
    "2012-11-13 09:03:49.174-08", "2012-11-13 06:42:10.208-08", 
    "2012-11-13 13:51:05.039-08", "2012-11-13 13:13:16.452-08", 
    "2012-11-13 12:42:08.917-08", "2012-11-13 12:28:51.541-08" 
    ), event = c("shipped", "statusCheck", "statusCheck", "statusCheck", 
    "status-picked", "warehouse_notified", "statusCheck", "recordCreated", 
    "shipped", "statusCheck", "statusCheck", "statusCheck", "status-picked", 
    "warehouse_notified", "statusCheck", "recordCreated", "shipped", 
    "statusCheck", "statusCheck", "statusCheck"), dateTime1 = structure(c(1352820337.947, 
    1352820162.614, 1352818996.62, 1352818895.149, 1352817941.964, 
    1352817897.664, 1352817897.644, 1352811915.725, 1352798556.427, 
    1352798407.202, 1352798096.182, 1352797900.438, 1352797431.571, 
    1352797431.461, 1352797429.174, 1352788930.208, 1352814665.039, 
    1352812396.452, 1352810528.917, 1352809731.541), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC")), .Names = c("custId", "date_time_recorded", 
"event", "dateTime1"), row.names = c(NA, 20L), class = "data.frame") 
+0

sì, non è CRAN. È possibile invece sostituire "as.POSIXct". Ho scaricato il file compresso e installato da lì, non il metodo 'install.packages ...'. – screechOwl

risposta

6

Questo funziona. Spero sia più veloce.

require(data.table) 
# convert this column to date format first 
ss1$date_time_recorded <- as.POSIXct(ss1$date_time_recorded) 
dt <- data.table(ss1, key="custId") 
dt[, .SD[dateTime1 < date_time_recorded[event == "shipped"]], by=custId] 

#  custId date_time_recorded   event   dateTime1 
# 1: 280510610 2012-11-13 06:42:10 recordCreated 2012-11-13 06:42:10 
# 2: 280511123 2012-11-13 12:42:08 statusCheck 2012-11-13 12:42:08 
# 3: 280511123 2012-11-13 12:28:51 statusCheck 2012-11-13 12:28:51 
# 4: 280512544 2012-11-13 13:05:15 recordCreated 2012-11-13 13:05:15 
+2

Fantastico! Molto più veloce. Grazie mille. – screechOwl