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:")
如果你用时间作为一个字符而不是因子来读表,例如用'fread' ...'df [,diff(as.ITime(time)),by = rep(1:nrow(df),每个= 2,length.out = nrow(df))]' – Frank
是的,它更优雅:) –