Skip to content

Instantly share code, notes, and snippets.

View joshbode's full-sized avatar

Josh Bode joshbode

  • Melbourne, Victoria, Australia
View GitHub Profile
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='')
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='')
@joshbode
joshbode / pmr.r
Last active December 13, 2015 23:59
Sub-totals for ddply
# 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, ...,
@joshbode
joshbode / gvim-git.sh
Last active December 14, 2015 09:08
Wrapper for git to use gvim under cygwin/msysgit for editing, diffing and merging. Handles quoting and path conversion.
#!/bin/sh
# gvim wrapper for git on cygwin and msysgit
# Josh Bode <[email protected]>
#
# .gitconfig setup:
#
# [core]
# editor = gvim-git.sh
#
@joshbode
joshbode / knitr
Last active December 14, 2015 09:18
Wrapper for knitr
#!/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)
@echo off
setlocal enabledelayedexpansion
net use G: "\\host\share" /persistent:yes
sc stop themes
sc stop mssql$sqlexpress
:: register fonts as local user
@joshbode
joshbode / coalesce.r
Last active December 14, 2015 18:49
Default missing values progressively in R
# 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)
# 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
#!/usr/bin/env python
# simple script to practice mental arithmetic
import random
import operator
import argparse
import re
answer_re = re.compile(r'^[-+]?\d+$')
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) +