我有一个DF包含时间戳和相应的"事件" (两种类型的事件),另一种具有时间戳和事件类型确认。
我试图计算事件和确认事件的时间之间的时间。
可以立即或稍后(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循环的情况下从相应的行中减去它?答案 0 :(得分:0)
定义输入dfEvent
和dfAcknowledgement
,然后使用适当的键转换为data.table,同时计算NextTime
。然后按Time
执行data.tables的滚动连接,仅保留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