我有一个DF,它包含时间戳和相应的“事件”类型(两种类型的事件),另一种包含时间戳和事件acknowledgements.类型。
我正在努力计算事件之间的时间,以及它被认可的时间。
确认可以立即收到,或者稍后接收(Tack>=Tevent
),或者永远不会收到。如果没有相应的确认,我希望时间差显示NA
示例:
dfEvent
Time Event
00:00:01 A
00:01:00 B
00:05:00 A
00:09:00 B
dfAcknowledgement
Time Event
00:00:02 A
00:05:10 A
00:09:05 B
RESULT
Time Event Delay
00:00:01 A 00:00:01
00:01:00 B NA
00:05:00 A 00:00:10
00:09:00 B 00:00:05
data.table
,但我不只是想合并表:我想要计算两个“附近”行的值之间的差异which()
来查找与确认相对应的事件的索引,但是一旦得到了索引,如何在没有for循环的情况下从相应的行中减去它呢?发布于 2014-07-16 12:20:24
定义输入dfEvent
和dfAcknowledgement
,然后使用相应的键转换为data.table,并计算NextTime
。然后执行data.tables的滚动连接,由Time
执行,只保留Time
、Event
和计算Delay
。
library(data.table)
library(chron)
# test data from question
dfEvent <- data.frame(Time = c("00:00:01", "00:01:00", "00:05:00", "00:09:00"),
Event = c("A", "B"), stringsAsFactors = FALSE)
dfAcknowledgement <- data.frame(Time = c("00:00:02", "00:05:10", "00:09:05"),
Event = c("A", "A", "B"), stringsAsFactors = FALSE)
# convert to data.table (also compute NextTime column)
dtEvent <- with(dfEvent, data.table(Event = Event, Time = times(Time),
NextTime = times(c(tail(Time, -1), NA)),
key = "Event,Time"))
dtAcknowledgement <- with(dfAcknowledgement, data.table(Event = Event,
Time = times(Time), TimeAck = times(Time),
key = "Event,Time"))
# real work done here - perform rolling join, re-sort & compute desired columns
roll <- dtAcknowledgement[dtEvent,,roll=-Inf]
setkey(setkey(roll, NULL), Time, Event)
roll[, list(Time, Event, Delay = replace(TimeAck, TimeAck>=NextTime, NA)-Time)]
这意味着:
Time Event Delay
1: 00:00:01 A 00:00:01
2: 00:01:00 B <NA>
3: 00:05:00 A 00:00:10
4: 00:09:00 B 00:00:05
https://stackoverflow.com/questions/24772535
复制相似问题