This file contains hidden or 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
fix_numbers = function(x) { | |
# strip out irrelevant characters | |
x = gsub('[^a-zA-Z0-9. ]', '', x) | |
# map SI units | |
si_map = c('k'=10^3, 'M'=10^6, 'G'=10^9) | |
for (unit in names(si_map)) { | |
zeros = paste0(rep('0', log10(si_map[unit])), collapse='') |
This file contains hidden or 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
fix_numbers = function(x) { | |
# strip out irrelevant characters | |
x = gsub('[^a-zA-Z0-9. ]', '', x) | |
# map SI units | |
si_map = c('k'=10^3, 'M'=10^6, 'G'=10^9) | |
for (unit in names(si_map)) { | |
zeros = paste0(rep('0', log10(si_map[unit])), collapse='') |
This file contains hidden or 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
# poor-man's rollup | |
# rollups like in SQL: GROUP BY ROLLUP(...) | |
# example: | |
# d = data.frame(x=trunc(runif(25, 1, 10)), y=trunc(runif(25, 1, 5)), z=rnorm(25)) | |
# result = pmr(d, .(x, y, j=x / 2), summarise, p=sum(z), .labels=list(x='-900', y='Total')) | |
require(plyr) | |
pmr = function(.data, .variables, ..., |
This file contains hidden or 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
#!/bin/sh | |
# gvim wrapper for git on cygwin and msysgit | |
# Josh Bode <[email protected]> | |
# | |
# .gitconfig setup: | |
# | |
# [core] | |
# editor = gvim-git.sh | |
# |
This file contains hidden or 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
#!/bin/bash | |
# wrapper for knitr | |
RMD_FILE="$1" | |
MD_FILE="/tmp/$(basename $0).$$.md" | |
HTML_FILE="${RMD_FILE%.*}.html" | |
read -d '' COMMAND << "EOF" | |
library(knitr, quietly=TRUE) |
This file contains hidden or 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
@echo off | |
setlocal enabledelayedexpansion | |
net use G: "\\host\share" /persistent:yes | |
sc stop themes | |
sc stop mssql$sqlexpress | |
:: register fonts as local user |
This file contains hidden or 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
# coalesce missing values | |
# e.g. | |
# > x = c(NA, 2, NA, 4) | |
# > y = c(1, NA, NA, 4) | |
# > z = c(1, NA, 3, 4) | |
# > coalesce(x, y, z) | |
coalesce = function(x, ...) { | |
result = x | |
for (y in list(...)) { | |
mask = is.na(result) |
This file contains hidden or 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
# Path functions ported to R from Python os.path module | |
path_join = function(a, ...) { | |
p = list(...) | |
path = a | |
for (b in p) { | |
b_wins = 0 # set to 1 iff b makes path irrelevant | |
if (path == "") { | |
b_wins = 1 |
This file contains hidden or 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
#!/usr/bin/env python | |
# simple script to practice mental arithmetic | |
import random | |
import operator | |
import argparse | |
import re | |
answer_re = re.compile(r'^[-+]?\d+$') |
This file contains hidden or 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(plyr) | |
library(ggplot2) | |
d = data.frame(YEAR=as.ordered(rep(c(2001:2010), 10)), val=rnorm(1000)) | |
p = ggplot(data=join(d, ddply(d, .(YEAR), function(x) { quantile(x$val) }))) + | |
geom_boxplot(aes(YEAR, val)) + | |
geom_text(aes(YEAR, `25%`, label=round(`25%`, 2)), stat='boxplot', vjust=-0.8, size=2) + | |
geom_text(aes(YEAR, `50%`, label=round(`50%`, 2)), stat='boxplot', vjust=-0.5, size=2) + | |
geom_text(aes(YEAR, `75%`, label=round(`75%`, 2)), stat='boxplot', vjust=+1.0, size=2) + |