R/dog_date_diff.R

Defines functions dog_date_diff

Documented in dog_date_diff

#' Difference between two timestamps.
#'
#' Calculates the difftime between two timestamps.
#'
#' @param df A data frame or tibble.
#' @param stamp1 Timestamp 1
#' @param stamp2 Timestamp 2
#' @param units  Units for the difftime function.
#'
#' @return A tibble
#' @export
#'
#'
dog_date_diff <- function(df, stamp1, stamp2, units = "mins"){

  stamp1 <- rlang::enquo(stamp1)
  stamp2 <- rlang::enquo(stamp2)

  name <- paste0(rlang::quo_name(stamp1), "_minus_", rlang::quo_name(stamp2))

  out <- df %>%
    dplyr::mutate(!!name := as.numeric(difftime(!!stamp1, !!stamp2, units=units)))

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