R/kcf_tab.R

Defines functions kcf_tab

Documented in kcf_tab

#' Tabulate frequency and relative frequency
#'
#' One-way and two-way tabulations of provided variables Speedboost via tidytable.
#' @param data The data frame to tabulate
#' @param tabvar The variables to tabulate
#' @param dropna If TRUE then drops NA values; if FALSE then includes NA values
#' @return A data frame with tabulation of counts and proportions.
#' @import tidytable
#' @examples
#' \dontrun{
#' kcf_tab(mtcars, c(vs)) # one-way tabulate
#' kcf_tab(mtcars, c(vs, am)) # two-way tabulate
#' }


kcf_tab <- function(data, tabvar, dropna = FALSE) {
  data |>
    {\(df) if (dropna == TRUE) tidytable::filter(df, tidytable::across(c({{ tabvar }}), ~ !is.na(.))) else df}() |>
    tidytable::count({{ tabvar }}, name = "n") |>
    tidytable::mutate(prop = n/sum(n))
}

.datatable.aware <- TRUE
kennchua/kctools documentation built on Aug. 28, 2024, 8:37 a.m.