Got rid of loops within add_case

This commit is contained in:
Nora Wickelmaier 2023-10-25 10:59:33 +02:00
parent 8bfa694cc9
commit 2340e081ff
1 changed files with 7 additions and 14 deletions

View File

@ -5,21 +5,14 @@
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[1], data$date.start)))
data$case <- NA
j <- 1
pb <- utils::txtProgressBar(min = 0, max = nrow(data), initial = NA, style = 3)
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
for (i in seq_len(nrow(data))) {
if (data$timediff[i] <= cutoff) {
data$case[i] <- j
} else {
j <- j + 1
data$case[i] <- j
}
utils::setTxtProgressBar(pb, i)
}
data$timediff <- NULL
data
}