From 5b0fc72761f073cd9f4e7985d3902b1125567110 Mon Sep 17 00:00:00 2001 From: Gufeng Zhou Date: Thu, 7 Oct 2021 20:12:41 +0200 Subject: [PATCH] update check_datevar, convert date_var to class iDate automatically --- R/R/checks.R | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/R/R/checks.R b/R/R/checks.R index 4edd5f8ae..d3ff3860b 100644 --- a/R/R/checks.R +++ b/R/R/checks.R @@ -70,21 +70,23 @@ check_datevar <- function(dt_input, date_var = "auto") { if (is.null(date_var) | length(date_var) > 1 | !(date_var %in% names(dt_input))) { stop("You must provide only 1 correct date variable name for 'date_var'") } - inputLen <- length(dt_input[, get(date_var)]) - inputLenUnique <- length(unique(dt_input[, get(date_var)])) + date_var_idate <- as.IDate(dt_input[, get(date_var)]) + dt_input[, (date_var):= date_var_idate] + inputLen <- length(date_var_idate) + inputLenUnique <- length(unique(date_var_idate)) if (inputLen != inputLenUnique) { stop("Date variable has duplicated dates. Please clean data first") } - if (any(is.na(as.Date(as.character(dt_input[, get(date_var)]), "%Y-%m-%d")))) { + if (any(is.na(date_var_idate))) { stop("Dates in 'date_var' must have format '2020-12-31'") } if (any(apply(dt_input, 2, function(x) any(is.na(x) | is.infinite(x))))) { stop("'dt_input' has NA or Inf. Please clean data before you proceed") } - dt_input <- dt_input[order(as.Date(dt_input[, get(date_var)]))] + dt_input <- dt_input[order(date_var_idate)] dayInterval <- as.integer(difftime( - as.Date(dt_input[, get(date_var)])[2], - as.Date(dt_input[, get(date_var)])[1], + date_var_idate[2], + date_var_idate[1], units = "days" )) intervalType <- if (dayInterval == 1) {