R/dog_date_from_text.R

Defines functions dog_date_from_text

Documented in dog_date_from_text

#' Change class
#'
#' Changes the class of a text timestamp to a POSIXct.
#'
#' @param df A data frame or tibble.
#' @param x  A column with datetime in text format.
#'
#' @return The same data frame with the timestamp column as datetime.
#' @export
#'
#' @examples
#' dog_date_from_text(tibble::tibble(time_text = as.character(Sys.time())), time_text)
dog_date_from_text <- function(df, x){
  x <- rlang::enquo(x)
  name <- rlang::quo_name(x)

 if(any(class(df[[name]]) %in% c("POSIXct", "POSIXt" ))) {

    return(df)

  }

  else if (class(df[[name]]) == "character") {

    df <- dplyr::mutate(df, !!name := lubridate::ymd_hms(!!x))
    return(df)

   }
  else {
    stop("check class of column manually")

  }

}
davidbaniadam/rispacs documentation built on Nov. 4, 2019, 9:43 a.m.