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
# uses dev version of 'waffle' | |
# devtools::install_github("leeper/waffle@patch-1") | |
library("waffle") | |
library("extrafont") | |
# population | |
set.seed(1) | |
N <- 900L | |
p <- c("Small Group 1" = 30, "Big Group 1" = 420, "Small Group 2" = 30,"Big Group 2" = 420) | |
glyph <- c("male", "female")[sample(1:2, N, TRUE)] |
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
Figure 4* | |
barplots* | |
codebook.pdf |
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("utils") | |
library("ggplot2") | |
library("ggrepel") | |
dat <- read.csv("data.csv", stringsAsFactors = FALSE) | |
dat[["total"]] <- dat[["democrat"]] + dat[["republican"]] | |
dat[["abbreviation"]] <- sapply(dat[["state"]], function(x) state.abb[grep(x,tolower(state.name))[1]]) | |
ggplot(dat, aes(x = democrat, y = republican, colour = total)) + | |
geom_point() + geom_abline(slope = 1, intercept = 0, colour = "gray") + |
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
`%<%` <- function(FUN, arg) { | |
f <- formals(FUN) | |
if (!length(f)) { | |
stop("No formal arguments in ", as.character(deparse(substitute(FUN)))) | |
} | |
f2 <- f[-1] | |
FUN2 <- function() { | |
arglist <- c(arg, as.list(match.call())[-1]) | |
do.call(FUN, arglist) | |
} |
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
# function to identify what changed where | |
which_changed <- function(x, y = x) { | |
# argument validation | |
stopifnot(inherits(x, "data.frame")) | |
stopifnot(inherits(y, "data.frame")) | |
stopifnot(identical(dim(x), dim(y))) | |
y <- y[, names(x)] | |
# compare objects |
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
get_all_estimates <- function(data, x, y, power = 1L) { | |
xvar <- x | |
assign(xvar, data[[xvar]]) | |
yvar <- y | |
assign(yvar, data[[yvar]]) | |
data <- data[, !names(data) %in% c(xvar, yvar), drop = FALSE] | |
datalist <- list() |
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
# excel windows and mac (see `as.Date()`) | |
# julian/gregorian; new style/old style | |
# mayan calendar | |
# armenian | |
# assyrian | |
# baha'i | |
# benghali | |
# berber | |
# buddhist | |
# burmese |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div> | |
<form> | |
<div> | |
<p>Mouse: <input type="text" id="xmove" /> | |
<input type="text" id="ymove" /> |
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
# Check URLs in a document | |
## This code will extract URLs from a text document using regex, | |
## then execute an HTTP HEAD request on each and report whether | |
## the request failed, whether a redirect occurred, etc. It might | |
## be useful for cleaning up linkrot. | |
if (!require("httr")) { | |
install.packages("httr", repos = "http://cran.rstudio.com/") | |
} |
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
set.seed(1) | |
n <- 300 | |
x <- rbinom(n, 1, .5) | |
y <- 4 + (2 * x) + rnorm(n, 0, 3) | |
m <- tapply(y, x, mean) | |
wm <- weighted.mean(c(mean(y[x==1]), mean(y[x==0])), c(length(y[x==1]), length(y[x==0]))) | |
ci <- function(formula, conf.level, var.equal = FALSE) { | |
(tt <- t.test(formula, var.equal = var.equal, conf.level = conf.level)) | |
rect(0, wm - diff(tt$conf.int)/2, |