Skip to content

Instantly share code, notes, and snippets.

druedin / plzcanton-tools.R
Created February 1, 2014 20:23
Two simple helper functions to use in conjuction with the postcode to canton converters at The first converts cantonal ID to their abbreviated labels, the second checks if a number is a vald Swiss postcode.
convid <- function(ID) {
# function to convert cantonal ID, v.1.0 (12 Nov 2013)
clabels <- c("ZH", "BE", "LU", "UR", "SZ", "OW", "NW", "GL", "ZG", "FR", "SO", "BS","BL", "SH", "AR", "AI", "SG", "GR", "AG", "TG", "TI", "VD", "VS", "NE", "GE", "JU")
id <- ifelse(is.numeric(ID), clabels[ID], match(ID, clabels))
plzvalid <- function(PLZ) {
# check if a number is a valid postcode; v.1.1 (1 Feb 2013)
druedin / splitworddocuments
Created April 12, 2014 20:28
This is a modified VBA script for splitting MS Word documents. The original is from apparently from (user lucas). I am unable to determine a licence, and share the code here to highight the few cha…
Attribute VB_Name = "Module1"
Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections. Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
druedin / fade-coefplot.R
Last active August 29, 2015 14:17
Shading Coefficient Plots in R
fade <- function(x, labels=names(coef(x)), expo=FALSE, xlab="", ylab="", bty="n", ...) {
# argument: a regression, additional arguments passed on to plot() and text()
coe <- summary(x)$coefficients[,1] # extract coefficients
cse <- summary(x)$coefficients[,2] # standard errors
len <- length(coe) # how many coefficients (without intercept)
if(expo == TRUE) { # exponential form
coe <- exp(coe)
cse <- exp(cse)
druedin / gist:4141631
Created November 24, 2012 22:19
German Stopwords
# Code by Matt Asher for
# Feel free to modify and redistribute
# How many flakes do you want to fall?
flakes = 100
# Width and height of your space
druedin / Demo ajusCheck.R
Created December 15, 2012 21:28
Demonstrating ajusCheck
library(agrmt) # install from R-Forge
# Data:
V <- c(0,0,1,2,1.5,1.6)
ajus(V) # using default tolerance = 0.1
ajusPlot(V) # visual inspection
ajus(V, tolerance=0.2) # exploring
ajusCheck(V, t=seq(0.1,2,0.1)) # let's try them all
druedin / missing.R
Last active December 11, 2015 08:39
Plot with missing values
miss <- ![country=="UK")
plot(which(miss),[country=="UK" & miss], col="red", type="b", lwd=2, axes=FALSE, xlim=c(1,16))
axis(1,at=c(1,6,11,16), labels=c("1995","2000","2005","2010"))
druedin / as_pdf.R
Created February 3, 2013 11:48
Modified to run on Linux; original from Will Lowe
as_pdf <- function(x){
fname <- tempfile(pattern = "texview-", tmpdir = tempdir(),
fileext = ".tex")
header <- "\\documentclass{article}
druedin / ethnic-and-racial-studies.csl
Last active December 14, 2015 12:49
CSL for Ethnic and Racial Studies (ESR), second draft. Author instructions at:
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="" class="in-text" version="1.0" page-range-format="chicago" demote-non-dropping-particle="sort-only">
<title>Ethnic and Racial Studies</title>
<link href="" rel="self"/>
<link href=";page=instructions" rel="documentation"/>
<name>Didier Ruedin</name>
druedin / namav
Created June 15, 2013 20:34
Moving average handling NA
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum([(i-k):(i+k)])))))