R/detect_date.R

Defines functions detect_date

Documented in detect_date

#' @title Identifies dates
#' 
#' @description A function to recognize if the content of a column seems to be a date.
#'  
#' @author Briac LE RAY (briac.leray@partnre.com)
#' 
#' @return TRUE if the column seems to contain dates, otherwise FALSE.
#' @param df_column The column to control. Type = dataframe
#' @keywords detect date detect_date() control
#' @examples detect_date(df_column = my_column)

detect_date <- function(df_column) {
  if (all(str_detect(df_column, "[0-9/]{8}"), na.rm = TRUE) & max(nchar(df_column[[names(df_column)]]), na.rm = TRUE) <= 10) {return(TRUE)} # Test format 31/12/20 or 31/12/2020
  if (all(str_detect(df_column, "[0-9-]{8}"), na.rm = TRUE) & max(nchar(df_column[[names(df_column)]]), na.rm = TRUE) <= 10) {return(TRUE)} # Test format 31-12-20 or 31-12-2020
  if (all(str_detect(df_column, "[0-9]{6}"), na.rm = TRUE) & max(nchar(df_column[[names(df_column)]]), na.rm = TRUE) <= 8) {return(TRUE)} # Test format 311220 or 31122020
  
  return(FALSE)
}
Tiipiac/StressTestOptimization documentation built on Dec. 18, 2021, 5:08 p.m.