Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

View GitHub Profile
d3 = function() {
var d3 = {
version: "3.2.7"
};
if (!Date.now) Date.now = function() {
return +new Date();
};
var d3_document = document, d3_documentElement = d3_document.documentElement, d3_window = window;
try {
d3_document.createElement("div").style.setProperty("opacity", 0, "");
@jrnold
jrnold / brackets.tex
Created December 6, 2016 00:46
Better names for LaTeX Bracket Sizes
\newcommand{\bigly}{\Big}
\newcommand{\BIG}{\bigg}
\newcommand{\NotoriousBIG}{\Bigg}
@jrnold
jrnold / compose2.R
Created December 2, 2016 21:35
Alternative R compose function
library(purrr)
compose2 <- function (...) {
fs <- lapply(list(...), match.fun)
n <- length(fs)
last <- fs[[n]]
rest <- fs[-n]
newf <- function(...) {
cl <- match.call()
cl[[1L]] <- last
@jrnold
jrnold / pollster_2016_election_cycle.R
Created March 26, 2016 05:06
Get all polls from the 2016 election cycle from Pollster API using the R package pollstR
library("pollstR")
library("dplyr")
# Get the list of available charts
charts <- pollstr_charts()
# Filter charts for any chart matching 2016
charts_2016_slugs <- grep("^2016", charts$charts$slug, value = TRUE)
charts_2016_slugs %>% head()
# These can be used when querying charts or polls
@jrnold
jrnold / foo.R
Created February 24, 2016 01:07
filter(ESS, ! (ESS$cntry %in% c('CZ', 'EE', 'HU', "PL", "SI", "SK") & essround == 1))
library("dplyr")
library("tidyr")
library("ggplot2")
# Create data frame with columns: country, variable, value
long_df_by_country <-
df %>%
select(country, var1, var2, var3) %>%
gather(variable, value, - country)
@jrnold
jrnold / bootstrap_groups.R
Created February 16, 2016 16:55
broom boostrap() function using groups
bootstrap <- function (df, m, block = FALSE) {
n <- nrow(df)
attr(df, "indices") <-
if (is(df, "grouped_df") && length(attr(df, "indices")) && block) {
replicate(m,
unlist(lapply(attr(df, "indices"),
function(x) sample(length(x), replace = TRUE)),
recursive = FALSE, use.names = FALSE),
simplify = FALSE)
} else {
@jrnold
jrnold / strpad.R
Created February 14, 2016 03:48
Padding a string with 0's
library("stringr")
str_pad(c(1, 12, 123, 1234, 12345), 5, side = "left", pad = "0")
@jrnold
jrnold / axis_text_size.R
Last active June 7, 2021 15:19
Changing axis title and text size in ggplot with theme_economist
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour=factor(gear))) +
facet_wrap(~am)
# Change the size of axis titles to be 2 x that of the default text size using function rel
# Change the size of the axis text to be 8 pt
p + theme_economist() + scale_colour_economist() +
theme(axis.title = element_text(size = rel(2)),
axis.text = element_text(size = 8)
@jrnold
jrnold / failures.R
Last active November 24, 2015 19:46
Code to generate spells, and times since last failure from a failure indicator variable
#' Code to generate spells, and times since last failure from failure indicator variables
library(dplyr)
fill_na <- function(x, fill = 0) {
x[is.na(x)] <- fill
x
}
spells_from_failures <- function(x) {
#' if an obs is the beginning of a new spell, the previous