R/dog_date_columns.R

Defines functions dog_date_columns

Documented in dog_date_columns

#' Date columns
#'
#' Creates all the elements from a timestamp. Use date_from_text first if the timestamp is character column.
#' @param df A data frame
#' @param x A column with a datetime in the form ymd_hms. Does not need to have a datetime class.
#'
#' @return A data frame that contains all the parts that can be extracted from a datatime column of a \code{data frame}.
#' \itemize{
#'  \item \code{year}
#'  \item \code{yday:} The number of the day in the year, e.g. January first is 1.
#'  \item \code{weekday:} Sunday is 1 and Monday is 2 and so on.
#'  \item \code{hour:}
#'  \item \code{hour_min:}
#'  \item \code{month:}
#'  \item \code{date:}
#' }
#' @export
#'
#'
#' @examples
#' dog_date_columns(tibble::tibble(time = paste(Sys.time())), time)
dog_date_columns <- function(df, x){
  x <- rlang::enquo(x)

  out <-  df %>%
    dog_date_from_text(!!x) %>%
      dplyr::mutate(year=lubridate::year(!!x),
           yday=lubridate::yday(!!x),
           weekday=lubridate::wday(!!x, label=FALSE),
           hour = lubridate::hour(!!x),
           hour_min= hms::as.hms(lubridate::force_tz(!!x)),
           week_num = lubridate::week(!!x),
           month = lubridate::month(!!x),
           date = lubridate::date(!!x))
 out

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