Skip to content

Instantly share code, notes, and snippets.

View mhkeller's full-sized avatar

Michael Keller mhkeller

View GitHub Profile
@mhkeller
mhkeller / get_scores.R
Created August 7, 2012 18:10 — forked from drewconway/get_scores.R
Function returns quater scores from Wikipedia Super Bown pages
# Function returns quater scores from Wikipedia Super Bown pages
get.scores<-function(numeral) {
# Base URL for Wikipedia
wp.url<-getURL(paste("http://en.wikipedia.org/wiki/Super_Bowl_",numeral,sep=""))
wp.data<-htmlTreeParse(wp.url, useInternalNodes=TRUE)
score.html<-getNodeSet(wp.data,"//table[@style='background-color:transparent;']")
score.table<-readHTMLTable(score.html[[1]])
score.table<-transform(score.table, SB=numeral)
return(score.table)
}
@mhkeller
mhkeller / gist:3834498
Created October 4, 2012 15:45 — forked from abelsonlive/gist:3751902
google geocoding API, R
Forked from @BrianAbelson
library('rjson') # For parsing json in R
library('RCurl') # For sending an http request
library('plyr') # For ddply
# Takes an data frame with a unique id column and an address column at the very least
# it returns a data frame with the uid, lat, lng and specificity of geocoding
# use the uid to join these results back to your data if you want
# you can also accomplish roughly the same thing with a for loop instead of ddply
@mhkeller
mhkeller / assignColors.R
Created November 2, 2012 19:12 — forked from abelsonlive/assignColors.R
assignColors.R
# this function takes an input numeric vector and
# partitions it into a set number of breaks
# it then assigns a color to each break via RColorBrewer
assignColors <- function(var,
n = 9, # number of colors / breaks
style = "jenks", # can be changed to other methods in "classIntervals"
pal = "RdYlBu", # Palettes from RColorBrewer
na_color ='#787878', # Color to give NA's
na_omit = FALSE, # Logical, argument above will be irrelevant if TRUE
@mhkeller
mhkeller / gist:4060805
Created November 12, 2012 17:46 — forked from jatorre/gist:3892665
How to do many updates in a single query WAY faster
UPDATE counties_results o SET pres_dem_pct = n.pres_dem_pct, pres_gop_pct = n.pres_gop_pct,
pres_oth_pct = n.pres_oth_pct, pres_pctrpt = n.pres_pctrpt
FROM ( VALUES
(48.51, 39.69, 11.8, 100, 'AK', '02000'),
(51.65, 41.67, 6.68, 100 , 'AL' , '01001' ),
(52.14, 42.4, 5.46, 100 , 'AL' , '01003' ),
(51.94, 42.24, 5.82, 100 , 'AL' , '01005' ),
(60.28, 34.95, 4.76, 100 , 'AL' , '01007' ),
(53.19, 42.04, 4.77, 100 , 'AL' , '01009' ),
(45.07, 50.85, 4.08, 100 , 'AL' , '01011' ),
d <- read.csv("all_posts.csv", stringsAsFactors=F)
pattern <- "regex here"
d$guns <- 0
d$guns[grep(pattern, d$text)] <- 1
d_guns <- d[d$guns==1,]
write.csv(d_guns, "gun_posts.csv", row.names=F)
<!DOCTYPE html>
<script src="http://mbostock.github.com/d3/d3.v2.js?2.8.1"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<style>
body {
font: 10px sans-serif;
}
rect {
<!DOCTYPE html>
<script src="http://mbostock.github.com/d3/d3.v2.js?2.8.1"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<style>
body {
font: 10px sans-serif;
}
rect {
@mhkeller
mhkeller / README.md
Created September 18, 2013 20:59 — forked from mbostock/.block

A line chart with mouseover so that you can read the y-value based on the closest x-value. The x-value is found using d3.mouse and scale.invert. The y-value is then found by bisecting the data.

@mhkeller
mhkeller / README.md
Last active February 23, 2016 15:29 — forked from mbostock/.block
Colorbrewer scales

A quick visual reference to every ColorBrewer scale; colors by Cynthia Brewer. Available in CSS and JS format. Click on a palette to log the constituent colors in hexadecimal RGB to the console.

Fork adds the ColorBrewer name as a label and the Spacegray 80s Dark palette.