R/is_daynight.R

Defines functions is_daynight

Documented in is_daynight

#' Is the time day or night.
#'
#' SUPPORTER function: Required for add_daynight function
#' This function supports the add_daynight function by creating a function called is_daynight. It first extracts the hour value from
#' a colon-separated 'character' or 'factor' value. Then it checks to see if the hour value is above or below 12. If it is above 12, then
#' it will print "night", if it is less, it will print "day"
#'
#'
#' @param t character or factor
#'
#' @return dataframe
#' @export
#' @examples
#' a <- "12:30:12"
#' b <- "4:40:50"
#' is_day_night(a)
#' ## "night"
#' is_day_night(b)
#' ## "day"


is_daynight <- function(t){
  hr <- t %>%
    as.character() %>%
    strsplit(.,split="\\:") %>%
    unlist() %>%
    .[1] %>%
    as.integer()
  if (hr >= 12) {
    tme <- "night"
  } else if (hr < 12) {
    tme <- "day"
  }

  tme
}
hochoy/r_timekeeper documentation built on May 17, 2019, 4:36 p.m.