Last active
February 25, 2022 18:34
-
-
Save vikjam/ef072ee77e0a74efd15fb308af09efbb to your computer and use it in GitHub Desktop.
Revisions
-
vikjam revised this gist
Jan 29, 2022 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { -
vikjam revised this gist
Jan 29, 2022 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,10 @@ 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 revised this gist
Jan 14, 2022 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,9 @@ library(dplyr) prop_table <- function(dat, vars) { dat %>% group_by(across({{vars}})) %>% tally() %>% ungroup() %>% mutate(prop = prop.table(n)) } -
vikjam renamed this gist
Jan 14, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) prop_table <- function(df, var) { df %>% group_by(var) %>% tally() %>% mutate(prop = prop.table(n)) } -
vikjam created this gist
Jan 14, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) }