如何在R中找到两个事件之间的时差?

时间:2017-04-20 12:29:18

标签: r data.table

我的数据框如下:

time    event   name
00:09:45    OUT:    abc
00:09:55    IN:     abc
03:14:52    OUT:    abc
03:15:02    IN:     abc
03:37:08    OUT:    abc
03:37:17    IN:     abc
03:39:17    OUT:    abc
03:39:26    IN:     abc

我想计算OUT和IN之间的时间差,这样我在第一个OUT和IN之间有10秒,在第二个OUT和IN之间有10秒。 有没有可行的办法呢?

2 个答案:

答案 0 :(得分:1)

可能有很多方法,下面是data.table

的一个选项
df<-read.table(text="time    event   name
00:09:45    OUT:    abc
00:09:55    IN:     abc
03:14:52    OUT:    abc
03:15:02    IN:     abc
03:37:08    OUT:    abc
03:37:17    IN:     abc
03:39:17    OUT:    abc
03:39:26    IN:     abc",header=T)

library(data.table)
setDT(df)
df[,id:=rep(1:(length(.I)/2),each=2)]
dfs<-dcast(df,name+id~event,value.var = "time")
dfs[,Difference:=difftime(strptime(`IN:`, "%H:%M:%S"),strptime(`OUT:`, "%H:%M:%S"))]

dfs
name id      IN:     OUT: Difference
1:  abc 1 00:09:55 00:09:45    10 secs
2:  abc 2 03:15:02 03:14:52    10 secs
3:  abc 3 03:37:17 03:37:08     9 secs
4:  abc 4 03:39:26 03:39:17     9 secs

答案 1 :(得分:-1)

library(dplyr)

# create data

df <- structure(list(time = structure(c(0.00677083333333333, 0.00688657407407407, 
                                        0.135324074074074, 0.135439814814815, 0.150787037037037, 0.150891203703704, 
                                        0.152280092592593, 0.152384259259259), 
                                      format = "h:m:s", class = "times"), 
                     event = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), 
                                       .Label = c("IN:", "OUT:"), class = "factor"), 
                     name = structure(c(1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L), .Label = "abc", class = "factor")), class = "data.frame", .Names = c("time", "event", "name"), row.names = c(NA, -8L))

# process 

df %>% mutate(diff = time - lag(time)) %>% 
filter(event == "IN:")