Skip to content

Instantly share code, notes, and snippets.

@drewconway
drewconway / drafter.R
Created August 19, 2013 15:25
Function used in model to do draft based on player rankings.
draft.sequence <- c()
rep.count <- floor((num.teams*15)/(2*num.teams))
draft.order <- rep(c(1:num.teams, rev(1:num.teams)), rep.count)
draft.order <- c(draft.order, 1:num.teams)
#Function for
make.pick <- function(team_num) {
team <- teams[[team_num]]
selection.made <- FALSE
selection.index <- 1
@drewconway
drewconway / league_format.R
Created August 19, 2013 15:22
Team format for Yhat Fantasy Football Challegne
# Model only works for 15-man rosters with this format of players!
num.teams <- 10
qb <- 1
rb <- 2
wr <- 2
te <- 1
flex <- 1
def <- 1
pk <- 1
bench <- 6
@drewconway
drewconway / deploy.R
Created August 19, 2013 15:18
Template for deploying draft predictions for Yhat Fantasy Football Challenge using R
library(yhatr)
model.require <- function() {
library(plyr)
}
model.transform <- function(df) {
df
}
@drewconway
drewconway / ur1twt.py
Created December 5, 2012 23:51
Retweets random people's first ever tweet -- at random. Fun!
#!/usr/bin/env python
# encoding: utf-8
"""
ur1twt.py
Description: Retweets random people's first ever tweet. Fun!
Created by ([email protected]) on
# Copyright (c) , under the Simplified BSD License.
# For more information on FreeBSD see: http://www.opensource.org/licenses/bsd-license.php
@drewconway
drewconway / google_geocode.R
Created June 18, 2012 14:05
Grab lat / lon from an address using Google Maps
# Get a lat/lon value for each address
geocode.addr <- function(city, country) {
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- getURL(paste(geo.url, URLencode(paste(city, country, collapse="+")), "&sensor=false", sep=""))
geo.json <- fromJSON(geo.text)
if(geo.json$status == "OK"){
return(geo.json$results[[1]]$geometry$location)
}
else{
@drewconway
drewconway / clock.R
Created August 12, 2011 20:36
Generate an animated clock in R with ggplot2
# Generate digitial clock face
first.nine <- c('00', '01', '02', '03', '04', '05', '06', '07', '08', '09')
hours <- c(first.nine, as.character(seq(10,23,1)))
mins <- c(first.nine, as.character(seq(10,59,1)))
time.chars.l <- lapply(hours, function(h) paste(h, ':', mins, sep=''))
time.chars <- do.call(c, time.chars.l)
# Generate analog clock face
hour.pos <- seq(0, 12, 12/(12*60))[1:720]
min.pos <-seq(0,12, 12/60)[1:60]
@drewconway
drewconway / clock.R
Created August 12, 2011 20:08
Generate an animated clock in R with ggplot2
# Generate digitial clock face
first.nine <- c('00', '01', '02', '03', '04', '05', '06', '07', '08', '09')
hours <- c(first.nine, as.character(seq(10,23,1)))
mins <- c(first.nine, as.character(seq(10,59,1)))
time.chars.l <- lapply(hours, function(h) paste(h, ':', mins, sep=''))
time.chars <- do.call(c, time.chars.l)
# Generate analog clock face
hour.pos <- seq(0, 12, 12/(12*60))[1:720]
min.pos <-seq(0,12, 12/60)[1:60]
@drewconway
drewconway / networkx_d3_example.py
Created July 25, 2011 16:47
A short example for using the D3 export feature. Using Mike Dewar (@mikedewar) Twitter social graph :)
import networkx as nx
from networkx.readwrite import d3_js
mikedewar = nx.read_graphml('mikedewar_rec.graphml')
# We need to relabel nodes as Twitter name if we want to show the names in the plot
label_dict = dict(map(lambda i : (mikedewar.nodes()[i], mikedewar.nodes(data=True)[i][1]['Label']), xrange(mikedewar.number_of_nodes())))
mikedewar_d3 = nx.relabel_nodes(mikedewar, label_dict)
# Export
@drewconway
drewconway / noticeEMail.py
Created April 12, 2011 20:42
Sends an email message through GMail once the script is completed. Developed to be used with AWS (or other system) so that instances can be terminated once a long job is done. Only works for those with GMail accounts.
#!/usr/bin/env python
# encoding: utf-8
import smtplib
from datetime import datetime
def noticeEMail(starttime, usr, psw, fromaddr, toaddr):
"""
Sends an email message through GMail once the script is completed.
@drewconway
drewconway / sim_pi.R
Created March 14, 2011 21:28
A function for simulating the value of Pi using Monte Carlo.
# For this exercise we assume to draw random points inside the square on the [-1,1] unit,
# and thus the value of Pi = 4(# random pts insid cirlce / # random pts in square)
sim.pi <- function(iterations = 1000) {
# Generate two vectors for random points in unit circle
x.pos <- runif(iterations, min=-1, max=1)
y.pos <- runif(iterations, min=-1, max=1)
# Test if draws are inside the unit circle
draw.pos <- ifelse(x.pos^2 + y.pos^2 <= 1, TRUE, FALSE)
draws.in <- length(which(draw.pos == TRUE))
# Estimate Pi