R/count_multiple.R

Defines functions count_multiple

Documented in count_multiple

#' Count multiple variables of the same data type
#'
#' @param df Data frame or tibble object
#' @param ... The variables you want to create individual tables for - unquoted and should be of the same data type.
#'
#' @return Summary frames which separately count inputted variables and add a percent column
#' @export
#'
count_multiple <- function(df, ...){

  df <- df %>%
    dplyr::select(...)%>%
    tidyr::pivot_longer(cols = everything())

  list_names <- df %>% dplyr::distinct(name) %>% dplyr::pull(1)
  list_names <- sort(list_names)

  list_output <- df %>%
    dplyr::group_split(name)%>%
    purrr::map(., ~ .x %>%
                 dplyr::count(value, sort = TRUE) %>%
                 dplyr::mutate(percent = 100 * n / sum(n)))

  names(list_output) <- list_names

  return(list_output)

}
jpcompartir/JPackage documentation built on March 20, 2023, 4 a.m.