R/add_datetime.R

Defines functions add_datetime

Documented in add_datetime

#' Adds date and time to a dataframe
#'
#' SUPPORTER function: Required for add_daynight function
#' This function adds two columns of date and time to all rows of a given dataframe. The main purpose is to log any new additions of rows
#' to the dataframe
#'
#' @param df dataframe
#'
#'
#' @return dataframe
#' @export
#' @examples
#' fruit_harvest <- data.frame(apples = c(4,10,12), oranges = c(8,9,10))
#' add_datetime(fruit_harvest)
#' ##   apples oranges       date     time
#' ## 1      4       8 2015-11-26 23:47:05
#' ## 2     10       9 2015-11-26 23:47:05
#' ## 3     12      10 2015-11-26 23:47:05

add_datetime <- function(df) {
  t <- Sys.time()
  dte <- as.Date(t)
  tme <- format(as.POSIXct(t),format = "%H:%M:%S" )

  if (!(any(colnames(df) == "time"))) {
    output_df <- cbind(df,
                       date = dte,
                       time = tme)
    output_df
  } else print("Error: There is already a time column")
}
hochoy/r_timekeeper documentation built on May 17, 2019, 4:36 p.m.