Skip to content

Instantly share code, notes, and snippets.

@johnjosephhorton
johnjosephhorton / get_rank.R
Created May 21, 2011 21:23
functional programming way to extract the within-subject order that some time-stamped event occurred
# accept time would be seconds from the unix epoch
results$rank <- mapply(
function(time,worker_id){
which(time==sort(subset(results, WorkerId==worker_id)$accept_time))
}, results$accept_time, results$WorkerId)
@johnjosephhorton
johnjosephhorton / cases.R
Created May 21, 2011 21:27
functional programming way to implement a case method in R
# keys and values map 1:1
# e.g.,
keys <- c(1,2,3)
values <- c('a','b','c')
list_of_keys <- c(1,2,3,1,1,2)
letters <- sapply(list_of_keys, function(x){values[which(x==keys)]})
@johnjosephhorton
johnjosephhorton / unix2POSIXct.R
Created July 20, 2011 16:40
Convert unix epoch time stamp into R datetime class
unix2POSIXct <- function (time) structure(time, class = c("POSIXt", "POSIXct"))
@johnjosephhorton
johnjosephhorton / bls_data_get.py
Created October 2, 2011 03:23
Get data from a poorly formatted BLS table using Python
import urllib2
import csv
FIRST_LINE = 11
LAST_LINE = 38
def get_level(l):
for i, char in enumerate(l):
if char != " ":
break
def f(x):
return x**2
@johnjosephhorton
johnjosephhorton / nfl_mashup.R
Created October 8, 2011 18:05
NFL Mashup with R
library(lme4)
library(ggplot2)
library(XML)
# grab the NFL data & compute the score difference (Home - Away)
nfl.raw <- read.csv("http://www.repole.com/sun4cast/stats/nfl2011stats.csv")
nfl.raw$delta <- with(nfl.raw, (ScoreOff - ScoreDef))
# fit the model
m <- lmer(delta ~ (1 | TeamName) + (1|Opponent), data = nfl.raw)
@johnjosephhorton
johnjosephhorton / optimal_apps.R
Created January 27, 2012 21:48
Optimal number of applications to send
df$s.star <- with(df, log(-c/log(1-q))/log(1-q))
df$s.star[df$s.star < 0] <- 0
g.optimal <- ggplot(df, aes(x = q, y = s.star, colour=factor(c))) + geom_line(aes(group=factor(c))) +
xlab("Probability that an application is successful") +
ylab("Optimal number of applications to send")
png("optimal_apps.png")
print(g.optimal)
dev.off()
@johnjosephhorton
johnjosephhorton / stereotypes_about_animals.py
Created February 4, 2012 21:02
Stereotypes about animals
import networkx as nx
import matplotlib.pyplot as plt
relationships = {
'cats':['cute', 'clean', 'curious', 'lazy'],
'children':['cruel', 'happy', 'mean', 'stupid'],
'cows':['fat', 'sacred to hindus', 'stupid', 'sacred'],
'dogs':['loyal', 'cute', 'loving', 'happy'],
'frogs':['happy', 'slimy', 'important', 'sensitive to pollution'],
'goldfish':['dirty', 'good', 'addicting', 'hard to keep alive'],
@johnjosephhorton
johnjosephhorton / .emacs
Created February 5, 2012 06:09
My .emacs file
(add-to-list 'load-path "~/.emacs.d/")
(add-to-list 'load-path (expand-file-name "~/elisp/org-mode/lisp"))
(add-to-list 'auto-mode-alist '("\\.\\(org\\ |org_archive\\|txt\\)$" . org-mode))
(setq org-agenda-files '("/tmp/test.org"))
(require 'org-install)
(require 'org-habit)
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
@johnjosephhorton
johnjosephhorton / india.R
Created March 6, 2012 21:35
Code for making a plot of contractor locations
library(ggplot2)
library(sqldf)
# using the dataset 'raw' wich is a list of contractors by lat, long
df <- sqldf("SELECT COUNT(*) AS num, LocLat, LocLong
FROM raw GROUP BY LocLat, LocLong")
g <- qplot(LocLong,LocLat, colour=num, size=log(num), data = df)
png("india.png")
print(g)
dev.off()