library(AVRCHelp)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Welcome to my vignette for the str_comb_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)

Summary of the Functions

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.

## Group Iris data by Species, and report summary statistics according to 
## different str_comb_Intv input. Then output table with kableExtra

tab = iris %>% dplyr::group_by(Species) %>% 
  dplyr::summarise(Length1 = str_comb_Intv(Sepal.Length, fun = mean),
                   Length2 = str_comb_Intv(Sepal.Length, fun = median,
                                          limits = c(0.2, 0.6), digits = 0),
                   Length3 = str_comb_Intv(Sepal.Length, fun = median,
                                          limits = 0:1, delim = "-"),
                   Length4 = str_comb_Intv(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(str_comb_Intv = 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 = str_comb_Prop(cyl == 4, out = "percent"),
                            Cyl2 = str_comb_Prop(cyl_chr, "4", out = "percent"),
                            Cyl3 = str_comb_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 = str_comb_Intv(mpg),
                   am  = str_comb_Prop(am == 1),
                   hp  = str_comb_Intv(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)

str_comb_NA(X)
str_comb_NA(X, zero2dash = F)

X[na.ind] = NA

str_comb_NA(X)
str_comb_NA(X, out = "percentage")


Ajfrick/AVRCHelp documentation built on May 28, 2019, 1:34 a.m.