mtt/R/add_case.R

19 lines
545 B
R

###########################################################################
# Add case variable
add_case <- function(data, cutoff = 20) {
# TODO: What is the best choice for the cutoff here?
data$timediff <- as.numeric(diff(c(data$date.start, utils::tail(data$date.start, 1))))
data$timeindex <- ifelse(data$timediff <= cutoff, 0, 1)
case_change <- diff(c(0, c(which(data$timeindex == 1), nrow(data))))
data$case <- rep(seq_along(case_change), case_change)
data$timediff <- NULL
data$timeindex <- NULL
data
}