Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
#rstats-ing all the things

Andrew Heiss andrewheiss

👨‍💻
#rstats-ing all the things
View GitHub Profile
View clrs.md
# Colors via http://clrs.cc/
clrs <- list(
  navy = "#001F3F",
  blue = "#0074D9",
  aqua = "#7FDBFF",
  teal = "#39CCCC",
  olive = "#3D9970",
  green = "#2ECC40",
  lime = "#01FF70",
View tidygeocoder.R
library(tidyverse)
library(tidygeocoder)
library(sf)
library(albersusa)
places <- tribble(
~name, ~address,
"My empty GSU office", "14 Marietta Street NW, Atlanta, GA 30303",
"My old BYU office", "155 East 1230 North, Provo, UT 84604",
"My old Duke office", "201 Science Dr, Durham, NC 27708"
View ggplot_pts.md
library(tidyverse)

# See https://stackoverflow.com/a/17313561/120898
pts <- function(x) {
  as.numeric(grid::convertUnit(grid::unit(x, "pt"), "mm"))
}

df <- tibble(x = 1:10, y = 1:10)
View broom_model_results.md
library(tidyverse)
library(broom)

cars <- cars %>% 
  mutate(speed.c = scale(speed, center = TRUE, scale = FALSE))

mod1 <- lm(dist ~ speed.c, data = cars)

tidy(mod1)
View dplyr_grouping_stuff.md
library(tidyverse)

# Proportion of different types of drives in the data
mpg %>% 
  group_by(drv) %>% 
  summarize(total = n()) %>% 
  # R automatically ungrouped - it'll always ungroup the last group (in this case drv)
  # Percentage - sum(total) is based on the ungrouped 3-row dataset
  mutate(prop = total / sum(total))
View sourdough_pct.md
library(glue)

build_recipe <- function(g_flour, pct_water = 0.7, pct_starter = 0.25, pct_salt = 0.02) {
  actual_flour <- g_flour
  actual_water <- pct_water * g_flour
  actual_starter <- pct_starter * g_flour
  actual_salt <- pct_salt * g_flour
  
  starter_split <- actual_starter / 2
View ologit_stuff.md
library(MASS) # Has to come first because of dplyr::select
library(tidyverse)
library(broom)
library(haven)

example_df <- read_stata("http://stats.idre.ucla.edu/stat/data/ologit.dta") %>% 
  mutate(apply = factor(apply, levels = 1:3, 
                        labels = c("unlikely", "somewhat likely", "very likely"), 
                        ordered = TRUE))
View ologit_sans_tidyverse.R
library(MASS) # For ordered logit with polr()
library(reshape2) # For reshaping data
library(ggplot2) # For fun plots
library(broom) # For converting models into data frames
library(haven) # For read_stata
example_df <- read_stata("http://stats.idre.ucla.edu/stat/data/ologit.dta")
example_df$apply <- factor(example_df$apply, levels = 1:3,
labels = c("unlikely", "somewhat likely", "very likely"),
ordered = TRUE)
View melded-p-values.md
library(tidyverse)
library(Amelia)
library(broom)

# Use the africa dataset from Amelia
data(africa)
set.seed(1234)
imp_amelia <- amelia(x = africa, m = 5, cs = "country", ts = "year", logs = "gdp_pc", p2s = 0)
View middle-earth.R
library(tidyverse)
library(sf)
# https://github.com/jvangeld/ME-GIS
coastline <- read_sf("ME-GIS-master/Coastline2.shp")
contours <- read_sf("ME-GIS-master/Contours_18.shp")
rivers <- read_sf("ME-GIS-master/Rivers.shp")
lakes <- read_sf("ME-GIS-master/Lakes.shp")
forests <- read_sf("ME-GIS-master/Forests.shp")
mountains <- read_sf("ME-GIS-master/Mountains_Anno.shp")
You can’t perform that action at this time.