Got rid of loops within add_case
This commit is contained in:
parent
8bfa694cc9
commit
2340e081ff
19
R/add_case.R
19
R/add_case.R
@ -5,21 +5,14 @@
|
|||||||
add_case <- function(data, cutoff = 20) {
|
add_case <- function(data, cutoff = 20) {
|
||||||
# TODO: What is the best choice for the cutoff here?
|
# TODO: What is the best choice for the cutoff here?
|
||||||
|
|
||||||
data$timediff <- as.numeric(diff(c(data$date.start[1], data$date.start)))
|
data$timediff <- as.numeric(diff(c(data$date.start, utils::tail(data$date.start, 1))))
|
||||||
data$case <- NA
|
data$timeindex <- ifelse(data$timediff <= cutoff, 0, 1)
|
||||||
j <- 1
|
case_change <- diff(c(0, c(which(data$timeindex == 1), nrow(data))))
|
||||||
pb <- utils::txtProgressBar(min = 0, max = nrow(data), initial = NA, style = 3)
|
data$case <- rep(seq_along(case_change), case_change)
|
||||||
|
|
||||||
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$timediff <- NULL
|
||||||
|
data$timeindex <- NULL
|
||||||
|
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user