From 2cd9085791a2092ee3a6b5a51d3480ecaf5c3e40 Mon Sep 17 00:00:00 2001 From: Steffen Ehrmann Date: Tue, 13 Feb 2024 16:44:48 +0100 Subject: [PATCH] properly handle dates --- R/helpers.R | 7 ++++++- R/setIDVar.R | 4 +++- man/setIDVar.Rd | 4 +++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/R/helpers.R b/R/helpers.R index e66b93e..3a2596c 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -37,7 +37,12 @@ for(i in seq_along(idVars)){ # set the desired type - class(input[[which(names(idVars)[i] == names(input))]]) <- idVars[[i]]$datype + + if(idVars[[i]]$datype == "Date"){ + input[[which(names(idVars)[i] == names(input))]] <- as.Date(input[[which(names(idVars)[i] == names(input))]], tryFormats = c("%Y-%m-%d"), optional = TRUE) + } else { + class(input[[which(names(idVars)[i] == names(input))]]) <- idVars[[i]]$datype + } } diff --git a/R/setIDVar.R b/R/setIDVar.R index 860c501..f610e93 100644 --- a/R/setIDVar.R +++ b/R/setIDVar.R @@ -9,7 +9,9 @@ #' @param name [\code{character(1)}]\cr Name of the new identifying variable. #' @param type [\code{character(1)}]\cr data type of the new identifying #' variable. Possible values are \code{"c/character"}, \code{"i/integer"}, -#' \code{"n/numeric"}, \code{"l/logical"}, \code{"D/date"} or \code{"_/skip"}. +#' \code{"n/numeric"}, \code{"l/logical"}, \code{"D/Date"} or \code{"_/skip"}. +#' For \code{"D/Date"}, the value has to follow the form \code{YYYY-MM-DD}, +#' where dates that don't match that are replaced by NA. #' @param value [\code{character(1)}]\cr In case the variable is an implicit #' variable (i.e., which is not in the origin table), specify it here. #' @param columns [\code{integerish(.)}]\cr The column(s) in which the diff --git a/man/setIDVar.Rd b/man/setIDVar.Rd index ea43d7b..5ea79fd 100644 --- a/man/setIDVar.Rd +++ b/man/setIDVar.Rd @@ -25,7 +25,9 @@ information).} \item{type}{[\code{character(1)}]\cr data type of the new identifying variable. Possible values are \code{"c/character"}, \code{"i/integer"}, -\code{"n/numeric"}, \code{"l/logical"}, \code{"D/date"} or \code{"_/skip"}.} +\code{"n/numeric"}, \code{"l/logical"}, \code{"D/Date"} or \code{"_/skip"}. +For \code{"D/Date"}, the value has to follow the form \code{YYYY-MM-DD}, +where dates that don't match that are replaced by NA.} \item{value}{[\code{character(1)}]\cr In case the variable is an implicit variable (i.e., which is not in the origin table), specify it here.}