How to compare time variables?

My data set has records with usage of website in "hh:mm:ss"
I want to compare time variables as stated below.

T1 <- "00:01:10"
T2 <- "00:00:30"

Following new feature I am adding to my Dataset.

Dataset$ByAvgDuration <- ifelse(AvgSessionDuration)>T1, "Long Stay", ifelse(AvgSessionDuration<T2, "Short Stay", "Medium Stay"))

I am facing issues in output as getting NA in all rows. How to do same ?

I hope this gets you on the right track :slightly_smiling_face:

# Function for creating random time points
get_time_points = function(start = "2018-01-01", end = "2018-01-02", n = 100){
  x = sample(seq(as.POSIXct(start), as.POSIXct(end), by = "mins"), n)
  return(x)
}

# Create dummy data
d = data.frame(arrive = get_time_points(), leave = get_time_points())
d$stay = difftime(d$arrive, d$leave, units = "mins")

# Annotate stay
d$duration = ifelse(d$stay > 60, "Long", ifelse(d$stay > 30, "Medium", "Short"))
2 Likes