library(ajfhelpR) knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Welcome to my vignette for the summ_X
functions. Currently there are 3, though I only really use 2 of them. First off, I recommend installing the following packages which I'll be using extensively in all my guides. And let's be honest, they're just pretty dang awesome.
## Packages used for demonstration # install.packages("tidyverse") # install.packages("kableExtra") # install.packages("pander") # install.packages("stringr") # install.packages("knitr") library(tidyverse) library(kableExtra) library(pander) library(rmarkdown) library(knitr) library(stringr)
These are some functions I wrote primarily for use in creating tables. Rather than repeatedly tell it how to summarise the data and piece it together in a string, I let the function to do the main brunt of work. Being a manager instead of a worker has its perks, after all.
In my opinion, these tend to work best when used in tandem with dplyr summarise
function with grouped data.
summ_interval
takes in continuous variables and outputs an estimate of centrality (your choice), along with an interval estimate (your choice), rounded to a number of digits (your choice), and seperated by a given delimeter (also your choice!).
summ_prop
takes in either a logical vector, or a character vector with a supplied category to compare to and counts the numebr of matches along with a proportion. If a logical vector is supplied, no category is needed. This can be output as percentage or a percent, and with or without a $\%$ sign. Of note as well, if you want to treat $0 (0)$ as too uninterseting to matter, you can have it return a single '-' instead. Currently in dev is to handle factors more approporiately.
summ_na
works similarly to summ_prop
except it works for counting tallying missingness.
summ_full
is an extension of st_comb_IQR that also includes the range. It's not particularly useful, but I needed it for a table once.
## Group Iris data by Species, and report summary statistics according to ## different summ_interval input. Then output table with kableExtra tab = iris %>% dplyr::group_by(Species) %>% dplyr::summarise(Length1 = summ_interval(Sepal.Length, fun = mean), Length2 = summ_interval(Sepal.Length, fun = median, limits = c(0.2, 0.6), digits = 0), Length3 = summ_interval(Sepal.Length, fun = median, limits = 0:1, delim = "-"), Length4 = summ_interval(Sepal.Length, fun = mean, limits = c(0.4, 0.6), delim = " ; ")) %>% t %>% tibble::as.tibble() ##Format Table more cleanly colnames(tab) = tab[1,] tab = tab[-1,] tab = tab %>% dplyr::mutate(summ_interval = 1:4) %>% dplyr::select(c(4,1:3)) knitr::kable(tab,format = 'html') %>% kableExtra::kable_styling(bootstrap_options = "hover")
##Check Count and proportion of observations in mt cars with certain number of ##cylinders, using logical vectors, and character vector with category mtcars = mtcars %>% dplyr::mutate(cyl_chr = as.character(cyl)) mtcars %>% dplyr::summarise(Cyl1 = summ_prop(cyl == 4, out = "percent"), Cyl2 = summ_prop(cyl_chr, "4", out = "percent"), Cyl3 = summ_prop(cyl_chr %in% c("4","8"), out = "percent", perc_disp = T)) %>% t %>% knitr::kable(format = 'html') %>% kableExtra::kable_styling(bootstrap_options = "hover")
##Group mtcars by number of cylinders, and then summarise MPG, Count w prop ## of Automatic Transmission, and Horsepower mtcars %>% dplyr::group_by(cyl) %>% dplyr::summarise(mpg = summ_interval(mpg), am = summ_prop(am == 1), hp = summ_interval(log2(hp+1), digits = 1, limits = 0:1)) %>% t %>% knitr::kable(format = 'html') %>% kableExtra::kable_styling(bootstrap_options = "hover")
X = 1:100 na.ind = sample(1:100, 15, replace = F) summ_na(X) summ_na(X, zero2dash = F) X[na.ind] = NA summ_na(X) summ_na(X, out = "percentage")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.