knitr::opts_chunk$set(comment = NA)
Sys.setenv(TZ = "US/Central")
# Load packages
library(tidyverse)
library(bfuncs)
data("mtcars")

Helpful websites:

Aggregation with dplyr

SO summarise with multiple return values

R for data science

I originally created the tabstat function awhile back. I didn't create it to work with dplyr pipes and it never worked with grouped variables. Need to address both of those issues below.

I can calculate the mean, and many other statistics for continuous variables using dplyr::summarise. However, dplyr::summarise can only be used to return single number statistics. Because 95% CI's do not fit this requirement, I have to build my own function.

Acutally, maybe I should just use the do function and make a note in r notes. still need to find a function that returns 95% CI's to use with "do".

Example 1: Mean no grouping

mtcars %>% 
  summarise(mean_mpg = mean(mpg))

Example 2: Mean with grouping

mtcars %>% 
  group_by(cyl) %>% 
  summarise(mean_mpg = mean(mpg))

Example 3: Tabstat (multiple return values) no grouping

mtcars %>% 
  do(tabstat(.$mpg, stats = c("n", "ci")))

Example 4: Tabstat (multiple return values) with grouping

mtcars %>% 
  group_by(cyl) %>% 
  do(tabstat(.$mpg, stats = c("n", "ci")))

So, using "do" isn't necessarily the most elegant solution ever, but it seems to work just fine.

sessionInfo()


brad-cannell/myFunctions documentation built on July 21, 2019, 5:48 p.m.