Changed add_case; now based on min and max of paths

This commit is contained in:
Nora Wickelmaier 2024-01-17 18:10:14 +01:00
parent 8233b151d9
commit b595c261c5
2 changed files with 27 additions and 7 deletions

View File

@ -5,14 +5,34 @@
add_case <- function(data, cutoff = 20) {
# TODO: What is the best choice for the cutoff here?
data$timediff <- -c(utils::head(data$date.stop, nrow(data) - 1) - data$date.start[-1], 0)
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)
pbapply::pboptions(style = 3, char = "=")
data$timediff <- NULL
data$timeindex <- NULL
dat_split <- split(data, ~ path)
time_minmax <- function(subdata) {
subdata$min_time <- min(subdata$date.start)
subdata$max_time <- ifelse(all(is.na(subdata$date.stop)), NA,
max(subdata$date.stop, na.rm = TRUE))
subdata
}
dat_list <- pbapply::pblapply(dat_split, time_minmax)
dat_minmax <- dplyr::bind_rows(dat_list)
dat_case <- dat_minmax[!duplicated(dat_minmax$path), ]
dat_case$timediff <- -c(difftime(utils::head(dat_case$max_time, nrow(dat_case) - 1),
dat_case$min_time[-1], units = "secs"), 0)
dat_case$timeindex <- ifelse(dat_case$timediff <= cutoff, 0, 1)
case_change <- diff(c(0, c(which(dat_case$timeindex == 1), nrow(dat_case))))
dat_case$case <- rep(seq_along(case_change), case_change)
npath <- table(data$path)
data <- data[order(data$path), ]
data$case <- rep(dat_case$case, npath)
data <- data[order(data$fileId.start, data$date.start, data$timeMs.start), ]
data
}

View File

@ -56,7 +56,7 @@ create_eventlogs <- function(data, xmlpath = NULL, case_cutoff = 20,
# Add path for move events ##############################################
cat("\n\n########## Adding path variable for move events... ##########", "\n")
dat3 <- add_path_moves(dat2)
dat3 <- add_path_moves(dat2, cutoff = case_cutoff)
# Add case variable ######################################################
cat("\n########## Adding case variable... ##########", "\n\n")