Skip to content

Instantly share code, notes, and snippets.

View andrewheiss's full-sized avatar
👨‍💻
#rstats-ing all the things

Andrew Heiss andrewheiss

👨‍💻
#rstats-ing all the things
View GitHub Profile
#!/usr/bin/env python
import timeit
with_any = """
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
things_to_find = ['abc', 'def']
matching = [i for i, x in enumerate(my_list) if any(thing in x for thing in things_to_find)]
"""
without_any = """
@andrewheiss
andrewheiss / question.md
Last active December 21, 2015 01:08
Question about running duplicity-backup as root (or on root-owned files)

I've moved the script to /usr/local/bin/duplicity-backup and the configuration file to /etc/duplicity-backup.conf and both are owned by root:root. Everything backs up just fine except for files owned by root, such as automysqlbackup files in /var/lib/automysqlbackup. For example, these directories are backed up:

Mon Aug 12 22:51:22 2013 var
Tue Aug 13 01:31:55 2013 var/lib
Tue Aug 13 07:35:42 2013 var/lib/automysqlbackup
Tue Aug 13 01:36:25 2013 var/lib/automysqlbackup/daily
Tue Aug 13 07:35:41 2013 var/lib/automysqlbackup/daily/somedatabase

But the actual files inside somedatabase aren't (like somedatabase_2013-08-13_07h35m.Tuesday.sql.gz). These files have 0600 permissions and are owned by root:root.

Process: Adobe InDesign CC [63926]
Path: /Applications/Adobe InDesign CC/Adobe InDesign CC.app/Contents/MacOS/Adobe InDesign CC
Identifier: com.adobe.InDesign
Version: 9.0.0.244 (9000)
Code Type: X86-64 (Native)
Parent Process: launchd [228]
User ID: 501
Date/Time: 2013-08-16 16:47:00.078 -0400
OS Version: Mac OS X 10.8.4 (12E55)
@andrewheiss
andrewheiss / script
Last active December 21, 2015 06:29
TextExpander and Markdown Footnotes
tell application "System Events"
key code 125 using command down -- this is "scroll to bottom"
keystroke return
keystroke return
keystroke "v" using command down -- this pastes the clipboard
keystroke ":"
keystroke space
end tell
@andrewheiss
andrewheiss / parity_binary_stuff.R
Created August 20, 2013 19:16
Use plyr to do some fancy parity calculations
library(plyr) # This is absolutely necessary!!
# Sample data
original.data <- data.frame(
id=c(0, 0, 0, 1, 1, 2, 2, 2),
age=c(15, 20, 22, 18, 25, 17, 21, 24),
parity=c(1, 2, 3, 1, 2, 1, 2, 3),
dead.sister=c(22, 22, 22, 25, 25, 19, 19, 19),
year=c(2000, 2005, 2007, 1997, 2004, 2003, 2007, 2010)
)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>Rmd</string>
</array>
<key>name</key>
<string>knitr (Markdown)</string>
@andrewheiss
andrewheiss / ps1.R
Created September 6, 2013 02:32
ps1
library(foreign)
library(car)
library(plyr)
library(lubridate)
star <- read.dta("~/Documents/Duke 2013-2014/Fall 2013/PubPol 604/Problem set 1/PS1.dta")
# Table 1, panel A
# Free lunch across kindergarten class types
prop.table(xtabs(~ gkclasst + gkfreelu, data=star), 1)[,1]
oneway.test(as.numeric(gkfreelu) ~ gkclasst, data=star, var.equal=TRUE) # Force as.numeric for ANOVA magic
@andrewheiss
andrewheiss / medals.R
Created January 17, 2014 14:27
Olympic medals in R
library(ggplot2)
library(reshape2)
library(plyr)
library(stargazer)
#------------
# Read data
#------------
medals <- read.csv("http://andhs.co/olympics")
@andrewheiss
andrewheiss / ut-crime-tinkering.R
Created January 19, 2014 01:49
Utah crime tinkering
library(RCurl)
library(ggplot2)
# Load data
myCsv <- getURL("https://raw.github.com/trobbins/crime-stats-utah/master/data/index-crimes-by-agency/2012.csv")
ut.crime <- read.csv(textConnection(myCsv))
# Get rid of commas
ut.crime$population <- as.numeric(gsub(",", "", ut.crime$population, fixed=TRUE))
ut.crime$burglary <- as.numeric(gsub(",", "", ut.crime$burglary, fixed=TRUE))
@andrewheiss
andrewheiss / Graphics code.R
Created January 27, 2014 21:07
EDA with R and ggplot
# Load ggplot (install it first if you need to)
library(ggplot2)
# Load built-in data
mtcars <- mtcars
movies <- movies
# Select 1000 of the movies
movies <- movies[sample(nrow(movies), 1000), ]
# If you want to use data from Stata, do the following: