Skip to content

Instantly share code, notes, and snippets.

@druedin
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 https://gist.github.com/ruedin/6690720. 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) didier.ruedin@wolfson.oxon.org
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))
return(id)
}
plzvalid <- function(PLZ) {
# check if a number is a valid postcode; v.1.1 (1 Feb 2013) didier.ruedin@wolfson.oxon.org
return(!is.na(plzcanton(PLZ)))
@druedin
druedin / splitworddocuments
Created April 12, 2014 20:28
This is a modified VBA script for splitting MS Word documents. The original is from http://www.extendoffice.com/documents/word/966-word-split-document-into-multiple-documents.html apparently from http://www.vbaexpress.com/kb/getarticle.php?kb_id=922 (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
druedin / fade-coefplot.R
Last active August 29, 2015 14:17
Shading Coefficient Plots in R
library(denstrip)
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
druedin / gist:4141631
Created November 24, 2012 22:19
German Stopwords
die
und
der
in
zu
für
mit
ist
sich
den
# Code by Matt Asher for statisticsblog.com
# Feel free to modify and redistribute
# How many flakes do you want to fall?
flakes = 100
# Width and height of your space
@druedin
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
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
druedin / missing.R
Last active December 11, 2015 08:39
Plot with missing values
miss <- !is.na(variable.name[country=="UK")
plot(which(miss), variable.name[country=="UK" & miss], col="red", type="b", lwd=2, axes=FALSE, xlim=c(1,16))
axis(2)
axis(1,at=c(1,6,11,16), labels=c("1995","2000","2005","2010"))
@druedin
druedin / as_pdf.R
Created February 3, 2013 11:48
Modified to run on Linux; original from Will Lowe http://conjugateprior.org/2013/01/no-more-ascii-art/
as_pdf <- function(x){
require(tools)
fname <- tempfile(pattern = "texview-", tmpdir = tempdir(),
fileext = ".tex")
header <- "\\documentclass{article}
\\usepackage[margin=10pt,font=small,labelformat=empty,
labelsep=none]{caption}
\\usepackage{dcolumn}
@druedin
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: http://www.tandfonline.com/action/authorSubmission?journalCode=rers20&page=instructions
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" page-range-format="chicago" demote-non-dropping-particle="sort-only">
<info>
<title>Ethnic and Racial Studies</title>
<id>http://www.zotero.org/styles/ethnic-and-racial-studies</id>
<link href="http://www.zotero.org/styles/ethnic-and-racial-studies" rel="self"/>
<link href="www.tandfonline.com/action/authorSubmission?journalCode=rers20&amp;page=instructions" rel="documentation"/>
<author>
<name>Didier Ruedin</name>
<email>didier.ruedin@wolfson.oxon.org</email>
@druedin
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(is.na(x[(i-k):(i+k)])))))
}