Skip to content

Commit

Permalink
update check_datevar, convert date_var to class iDate automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
gufengzhou committed Oct 7, 2021
1 parent 118a9bf commit 5b0fc72
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions R/R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5b0fc72

Please sign in to comment.