This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\newcommand{\bigly}{\Big} | |
\newcommand{\BIG}{\bigg} | |
\newcommand{\NotoriousBIG}{\Bigg} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter(ESS, ! (ESS$cntry %in% c('CZ', 'EE', 'HU', "PL", "SI", "SK") & essround == 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("stringr") | |
str_pad(c(1, 12, 123, 1234, 12345), 5, side = "left", pad = "0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |