Skip to content

Instantly share code, notes, and snippets.

@vikjam
Last active February 25, 2022 18:34
Show Gist options
  • Save vikjam/ef072ee77e0a74efd15fb308af09efbb to your computer and use it in GitHub Desktop.
Save vikjam/ef072ee77e0a74efd15fb308af09efbb to your computer and use it in GitHub Desktop.
Proportions table in dplyr
#'@title Pipe-friendly (simplified) prop.table
#'@description \code{prop_table} Computes proportions similiar to prop.table using \code{dplyr} functions.
#'@param dat The data used for the computation.
#'@param vars The columns to create aggregate counts of.
#'@param na.rm Whether to remove missing values or not. Default is \code{FALSE}.
#'@return A \code{data.frame} with the counts and proportions by \code{vars}.
#'
#'@author vikjam
#'
#'@examples
#' example_df <- data.frame(
#' x1 = sample(c("A", "B", "C", NA), size = 100, replace = TRUE),
#' x2 = sample(c(1:5, NA), size = 100, replace = TRUE),
#' x3 = runif(100)
#' )
#' example_df %>% prop_table(c(x1, x2))
#'@export
library(dplyr)
prop_table <- function(dat, vars, na.rm = FALSE) {
dat %>%
group_by(across({{vars}})) %>%
tally() %>%
ungroup() %>%
{if (na.rm) na.omit(.) else .} %>%
mutate(prop = prop.table(n))
}
@vikjam
Copy link
Author

vikjam commented Jan 14, 2022

@vikjam
Copy link
Author

vikjam commented Feb 25, 2022

Probably could just use janitor::tabyl. For example,

set.seed(192)

example_df <- data.frame(
    x1 = sample(c("A", "B", "C", NA), size = 100, replace = TRUE),
    x2 = sample(c(1:5, NA), size = 100, replace = TRUE),
    x3 = runif(100)
)

example_df %>% tabyl(x1, x2)
#>   x1 1 2 3 4 5 NA_
#>    A 6 4 3 1 4   6
#>    B 3 2 0 3 2   7
#>    C 4 5 5 5 7   7
#> <NA> 1 6 4 4 5   6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment