R/dog_double_count.R

Defines functions dog_double_count

Documented in dog_double_count

#' Double Count Two Times
#'
#' This function takes a date column and a categorical variable and returns a data frame with the number
#'
#' @family Special barplot
#' @seealso \code{\link{cat_barplot_double_count}} for creating the input data frame.
#'
#' @param df A data frame with at least two columns.
#' @param date_col A date column.
#' @param categorie_col A column with a categorie.
#'
#' @return A data frame with three columns:
#' \itemize{
#'  \item The \code{categorie} column specified in the arguments.
#'  \item \code{n:} is a count of the \code{categorie} column (Is useless without the next column).
#'  \item \code{nn:} Count of number of days. This column denotes the count for the number of days
#'  that \code{categorie} had a count of \code{n}.
#' }
#'
#' @export
#'
#' @examples
#' test <- nycflights13::flights %>%
#' dplyr::mutate(date = lubridate::as_date(paste(year, month, day, sep="-")))
#' head(dog_double_count(df = test, date_col = date, categorie_col = carrier))
#'
dog_double_count <- function(df, date_col, categorie_col) {
  date_col <- rlang::enquo(date_col)
  categorie_col <- rlang::enquo(categorie_col)


  df %>%
    filter(!is.na(!!date_col), !is.na(!!categorie_col)) %>%
    count(!!date_col, !!categorie_col) %>%
    tidyr::spread(!!categorie_col, n) %>%
    replace(is.na(.), 0) %>%
    tidyr::gather(-!!date_col, key= !! as_label(categorie_col), value="n") %>%
    count(!!categorie_col, n)
}
davidbaniadam/rispacs documentation built on Nov. 4, 2019, 9:43 a.m.