I would like to determine the difference between timestamps regading a userID. Here I just want to measure the difference between those users who have a login und logout status. There are some users haven only logout our login status. For them I just would like to mark dem as NA
:
Some data:
library(dplyr)
start <- as.POSIXct("2012-01-15")
interval <- 70
end <- start + as.difftime(1, units="days")
tseq<- seq(from=start, by=interval*70, to=end)
employeID <-c("1_e","1_e","2_b","2_b","3_c","3_c","100_c","4_d","4_d","52_f","9_f","9_f","7_u","7_u","10_5","22_2","33_a","33_a")
status<- c("login","logout","login","logout","login","logout","login","logout","login","logout","login","logout","login","logout","logout","login","logout","login")
# put together
data <- data.frame(tseq, employeID, status)
tseq employeID status
#1 2012-01-15 00:00:00 1_e login
#2 2012-01-15 01:21:40 1_e logout
#3 2012-01-15 02:43:20 2_b login
#4 2012-01-15 04:05:00 2_b logout
#5 2012-01-15 05:26:40 3_c login
#6 2012-01-15 06:48:20 3_c logout
#7 2012-01-15 08:10:00 100_c login
#8 2012-01-15 09:31:40 4_d logout
#9 2012-01-15 10:53:20 4_d login
#10 2012-01-15 12:15:00 52_f logout
#11 2012-01-15 13:36:40 9_f login
#12 2012-01-15 14:58:20 9_f logout
#13 2012-01-15 16:20:00 7_u login
#14 2012-01-15 17:41:40 7_u logout
#15 2012-01-15 19:03:20 10_5 logout
#16 2012-01-15 20:25:00 22_2 login
#17 2012-01-15 21:46:40 33_a logout
#18 2012-01-15 23:08:20 33_a login
test<- data %>%
group_by(employeID) %>%
mutate(time.difference = tseq - lag(tseq))
However, that seems only to produce a time.difference constant