Skip to content

Instantly share code, notes, and snippets.

View jthomasmock's full-sized avatar

Tom Mock jthomasmock

View GitHub Profile
@jthomasmock
jthomasmock / README.md
Created June 21, 2017 16:34 — forked from jxson/README.md
README.md template

Synopsis

At the top of the file there should be a short introduction and/ or overview that explains what the project is. This description should match descriptions added for package managers (Gemspec, package.json, etc.)

Code Example

Show what the library does as concisely as possible, developers should be able to figure out how your project solves their problem by looking at the code example. Make sure the API you are showing off is obvious, and that your code is short and concise.

Motivation

library(tidyverse)
library(writexl)
library(broom)
mtcars %>%
mutate(cyl = factor(cyl),
am = factor(am)) %>%
select(disp:wt) %>%
map(~ summary(lm(.x ~ cyl * am, data = mtcars))) %>%
map( ~ tidy(.x)) %>%
writexl::write_xlsx(., "mtcars_example.xlsx")
@jthomasmock
jthomasmock / More purrr goodness: map2 and pwalk
Created March 2, 2018 00:55
Part of the purrr resolution by Isabella R. Ghement, Ph.D.
#=======================================================================
# Install required packages
#=======================================================================
install.packages("ggridges")
#=======================================================================
# Load required packages
#=======================================================================
@jthomasmock
jthomasmock / model_to_excel
Last active March 2, 2018 14:48
With tidyverse, purrr and writexl, you can generate many models simultaneously and save them to individual sheets in Excel!
library(tidyverse)
library(writexl)
library(broom)
mtcars %>%
select(disp:wt) %>%
map(~ summary(lm(.x ~ factor(cyl) * factor(am), data = mtcars))) %>%
map( ~ tidy(.x)) %>%
writexl::write_xlsx(., "mtcars_example.xlsx")
library(tidyverse)
library(beepr)
beep(4,mtcars %>%
group_by(am, cyl) %>%
summarize_at(vars(wt, mpg), funs(mean, sd)) %>%
print())
@jthomasmock
jthomasmock / groups.rmd
Created April 12, 2018 20:49
code to find group data
---
title: 'Find the #Group'
output:
html_document:
df_print: paged
---
```{r}
library(tidyverse)
```
@jthomasmock
jthomasmock / tidyr_fill.r
Created April 13, 2018 13:32
Tidyr::fill example
# generate dataset with 1x year per every 12 months
df <- data.frame(Month = rep(1:12, 3),
Year = c(2000, rep(NA, 11),
2001, rep(NA, 11),
2002, rep(NA, 11)))
# view the funky dataset
df
# fill the dataset with missing year!
@jthomasmock
jthomasmock / db_plot overlay
Last active May 16, 2018 12:40
dumbell_plot overlay
library(tidyverse)
# generate random data
xdf <- data.frame(id = rep(1:100, 2),
risk = c(rnorm(100, mean = 10, sd = 1), rnorm(100, mean = 7, sd = 1)),
year = c(rep(2016, 100), rep(2017, 100))) %>%
mutate(id = factor(id),
year = factor(year))
# summarize data
#week 21
library(tidyverse)
url <- "https://raw.githubusercontent.com/TheUpshot/chipotle/master/orders.tsv"
df <- read_tsv(url)
tidy_df <- df %>%
separate(choice_description, c("Filling 1", "Filling 2", "Filling 3", "Filling 4", "Filling 5", "Filling 6",
"Filling 7", "Filling 8", "Filling 9", "Filling 10"),
extra = "merge",
@jthomasmock
jthomasmock / glue::glue example
Created August 16, 2018 18:34
Basic example of glue usage
tribble_df <- tribble(~day, ~person, ~action,
"Monday", "Tom", "sprinting",
"Tuesday", "Susan", "strolling",
"Saturday", "Wonder Woman", "flying")
tribble_df %>%
mutate(glue_output = glue::glue("On {day} I realized {person} was {action} by the street"))