I've created a dataframe with data :
idCol <- c('1','1','2','2')
stepCol <- c('step1' , 'step2' , 'step1' , 'step2')
timestampCol <- c('01-01-2017:09.00', '01-01-2017:10.00', '01-01-2017:09:00', '01-01-2017:14.00')
mydata <- data.frame(idCol , stepCol , timestampCol)
colnames(mydata) <- c('id' , 'steps' , 'timestamp')
stepCol is the start time for a given id, when step2 begins this means step1 has ended.
I'm attempting to generate a tibble that contains the average of the duration for each id based on step start time.
So I'm attempting to generate :
step , averagetime
step1 , 1 hour
step2 , 5 hours
Closest I've got is :
diffTime <- c(0, difftime(ymd_hms(mydata$timestamp[-1]), ymd_hms(mydata$timestamp[-nrow(mydata)]), units="hours"))
diffTime %>% group_by(id, steps) %>% summarize(mean(diffTime))
But returns error :
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "c('double', 'numeric')"
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…