R/fix_dates.R

#' Fix a character field to date
#'
#' This function will return a dataframe with all
#' @param df dataframe you want add information to
#' @param format formt that date is currently in so we can parse it
#' @param date_search character string to search for within the names of each column; Any that match will be changed to a date format
#' @export
#' @examples
#' fix_dates(myydataframe)

fix_dates<-function(df, format="%Y-%m-%d %H:%M:%S", date_search="date"){
  if (!requireNamespace("lubridate", quietly = TRUE)) {
    stop("lubridate needed for this function to work. Please install it.",
         call. = FALSE)
  }
  library(lubridate)


  df[,grep(date_search,
           colnames(df))] <- lapply(df[,grep(date_search,
                                             colnames(df))],
                                    fast_strptime,
                                    format = format,
                                    lt=F)
  return(df)
}
keithabailey/PRMTools documentation built on May 20, 2019, 8:45 a.m.