Skip to content

Instantly share code, notes, and snippets.

druedin

Block or report user

Report or block druedin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@druedin
druedin / R
Created Aug 29, 2019
That quadruple sapply()
View R
# Hi Glen. Here is comes:
countries <- unique(country)
everything <- sapply(countries, function(cy) {
parties <- unique(party[country==cy]) # all parties
this <- sapply(parties, function(z) sapply(1990:2013, function(y) mav(as.numeric(sapply(seq(y-3,y+3), function(x) experts.raw[country==cy & party==z & year==x])))))
rownames(this) <- 1990:2013
colnames(this) <- parties
return(this)
})
print(everything )
@druedin
druedin / gist:91d0e6d2fc45444336da
Created Apr 23, 2015
Solarized (Dark) syntax highlighting for Pandoc/Markdown in Notepad++ (add this to userDefineLang.xml); can't recall which source file I modified... happy to give credit!
View gist:91d0e6d2fc45444336da
<NotepadPlus>
<UserLang name="Markdown" ext="markdown mdown mkdn mdwn mkd md Rmd" udlVersion="2.1">
<Settings>
<Global caseIgnored="yes" allowFoldOfComments="no" foldCompact="no" forcePureLC="2" decimalSeparator="0" />
<Prefix Keywords1="yes" Keywords2="yes" Keywords3="no" Keywords4="yes" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
</Settings>
<KeywordLists>
<Keywords name="Comments">00# 00## 00### 00#### 00##### 00###### 01 02 03[ 03![ 03&lt;!-- 04] 04] 04--&gt;</Keywords>
<Keywords name="Numbers, prefix1"></Keywords>
<Keywords name="Numbers, prefix2"></Keywords>
@druedin
druedin / fade-coefplot.R
Last active Aug 29, 2015
Shading Coefficient Plots in R
View fade-coefplot.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 / splitworddocuments
Created Apr 12, 2014
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…
View splitworddocuments
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 / plzcanton-tools.R
Created Feb 1, 2014
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.
View plzcanton-tools.R
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 / plzcanton.R
Last active Sep 6, 2016
Convert Swiss postcode (Postleitzahl, PLZ) to cantons. Essentially this is a database with information which postcodes are in which canton. For 16 postcodes, it is not possible to assign the canton unambiguously; I have assigned one of the cantons according to the largest municipality/hamlet involved -- usually ignoring hamlets. Source of underl…
View plzcanton.R
plzcanton <- function(PLZ, format=1) {
# convert Swiss postcode to canton; v.0.3 (12 Nov 2013) didier.ruedin@wolfson.oxon.org
# format 1 = numeric
# format 2 = string
# DATA: BFS canton numbers:
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")
# DATA: postcodes:
ZH <- c(8970, 8955, 8954, 8953, 8952, 8951, 8942, 8934, 8933, 8932, 8926, 8925, 8915, 8914, 8913, 8912, 8911, 8910, 8909, 8908, 8907, 8906, 8904, 8903, 8902, 8901, 8833, 8825, 8824, 8820, 8816, 8815, 8813, 8812, 8810, 8805, 8804, 8803, 8802, 8801, 8800, 8714, 8713, 8712, 8708, 8707, 8706, 8704, 8703, 8702, 8700, 8639, 8637, 8636, 8635, 8634, 8633, 8632, 8630, 8627, 8626, 8625, 8624, 8623, 8622, 8621, 8620, 8618, 8617, 8616, 8615, 8614, 8613, 8612, 8610, 8608, 8607, 8606, 8605, 8604, 8603, 8602, 8600, 8548, 8545, 8544, 8543, 8542, 8523, 8499, 8498, 8497, 8496, 8495, 8494, 8493, 8492, 8489, 8488, 8487, 8486, 8484, 8483, 84
View gist:5842378
library(psych)
n <- 5000
x <- rnorm(n) # random values
r <- sapply(1:n, function(i) alpha(data.frame(x, c(x[1:i],rep.int(0,n-i))))$total$std.alpha)
plot(r, type="l", ylim=c(0,1), xlim=c(0,n), axes=FALSE, ylab="Alpha", xlab="Zero-Inflation (%)")
axis(2)
axis(1, at=seq(from=0,to=n,by=n/5), labels=seq(from=0,to=100,by=20))
@druedin
druedin / namav
Created Jun 15, 2013
Moving average handling NA
View namav
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)])))))
}
View ethnic-and-racial-studies.csl
<?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 / as_pdf.R
Created Feb 3, 2013
Modified to run on Linux; original from Will Lowe http://conjugateprior.org/2013/01/no-more-ascii-art/
View as_pdf.R
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}
You can’t perform that action at this time.