Skip to content

Instantly share code, notes, and snippets.

@vikjam
Last active February 25, 2022 18:34
Show Gist options
  • Select an option

  • Save vikjam/ef072ee77e0a74efd15fb308af09efbb to your computer and use it in GitHub Desktop.

Select an option

Save vikjam/ef072ee77e0a74efd15fb308af09efbb to your computer and use it in GitHub Desktop.

Revisions

  1. vikjam revised this gist Jan 29, 2022. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions prop_table.r
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,20 @@
    #'@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) {
  2. vikjam revised this gist Jan 29, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion prop_table.r
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    library(dplyr)

    prop_table <- function(dat, vars) {
    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))
    }
  3. vikjam revised this gist Jan 14, 2022. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions prop_table.r
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    library(dplyr)

    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))

    prop_table <- function(df, var) {
    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))
    prop_table <- function(dat, vars) {
    dat %>%
    group_by(across({{vars}})) %>%
    tally() %>%
    ungroup() %>%
    mutate(prop = prop.table(n))
    }
  4. vikjam renamed this gist Jan 14, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dplyr_prop_table.r → prop_table.r
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,6 @@ library(dplyr)

    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))

    dplyr_prop_table <- function(df, var) {
    prop_table <- function(df, var) {
    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))
    }
  5. vikjam created this gist Jan 14, 2022.
    7 changes: 7 additions & 0 deletions dplyr_prop_table.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    library(dplyr)

    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))

    dplyr_prop_table <- function(df, var) {
    df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n))
    }