Skip to content

Instantly share code, notes, and snippets.

@cigrainger
cigrainger / dissertationgist
Last active December 21, 2015 12:49
Dissertation code, CEX data
require(RSQLite)
require(mitools)
require(stringr)
require(plyr)
#Import data and create panel data set
for(i in 2007:2011){
setwd("~/Documents/CEX0711")
year <- i
@cigrainger
cigrainger / dissertationprices
Created August 22, 2013 23:46
Dissertation
prices <- read.csv("~/Documents/CEX0711/prices.csv")
colnames(prices) <- sub("[.]", " ", colnames(prices))
prices <- melt(prices)
prices$Date <- as.yearmon(prices$Date,format="%b-%Y")
colnames(prices) <- c('time','statename','price')
prices$year <- format(prices$time,format="%Y")
statetax <- read.csv("~/Documents/CEX0711/statetax.csv")
colnames(statetax)[1] <- "statename"
colnames(statetax) <- sub("[X]", "", colnames(statetax))
@cigrainger
cigrainger / disspriceimpute
Created August 23, 2013 15:29
price imputation
#Impute price data
prices <- read.csv("~/Documents/CEX0711/prices.csv")
colnames(prices) <- sub("[.]", " ", colnames(prices))
prices <- melt(prices)
prices$Date <- as.yearmon(prices$Date,format="%b-%Y")
colnames(prices) <- c('time','statename','price')
prices$year <- format(prices$time,format="%Y")
statetax <- read.csv("~/Documents/CEX0711/statetax.csv")
colnames(statetax)[1] <- "statename"
@cigrainger
cigrainger / gist:7546558
Last active December 28, 2015 18:59
Clean up world development indicator data for analysis (esp in Stata)
prep.wdi <- function(x,cache=FALSE,filename=paste(Sys.time()),country='name',format='wide'){
require(reshape2)
if (!is.character(x)) {stop('x must be a string.')}
df <- read.csv(x)
if (names(df)[1] != 'Country.Name' |
names(df)[2] != 'Country.Code' |
names(df)[3] != 'Indicator.Name' |
names(df)[4] != 'Indicator.Code') {
stop('This is not a properly formatted WDI csv.')
}
quarterCalc <- function(interviewTime, interviewState, priceData){
if(!inherits(interviewTime, "yearmon")){
stop("Wrong date type")
}
newData <- subset(priceData, as.character(priceData$statename)==as.character(interviewState))
if(dim(subset(priceData, time==interviewTime & as.character(statename)==as.character(interviewState)))[1] > 1){return(NA)}
index = which(newData$time == interviewTime, arr.ind=TRUE)
if(length(index)>1){ stop(print(index))}
else if(index>=3){
quarterly <- data.frame("time" = interviewTime, "state" = interviewState, "price" = mean(newData$nprc[seq((index-3), (index-1), 1)], na.rm=TRUE))
import pandas as pd
students = pd.read_csv('students.csv')
def newstudent(x):
newname = raw_input("What is the student's name? ")
newemail = raw_input("What is the student's email address? ")
newrow = [{'name':newname,'email':newemail}]
df = x.append(newrow, ignore_index=True)
return df
def addstudent(x):
newdf = newstudent(x)
import csv
f = open('students.csv').read()
while f.split('\n')[0] != 'name,email':
morecsv = raw_input("This does not look like the correct file. Would you like to see more? y/n?")
while morecsv != 'y' or 'n':
morecsv = raw_input("The prompt was y/n only. Would you like to see more? y/n?")
if morecsv == 'y':
print f.split('\n')[:10]
newcsv = raw_input("Would you like to overwrite this file to create the student list? y/n?")
while newcsv != 'y' or 'n':
import csv
f = open('students.csv').read()
while f.split('\n')[0] != 'name,email':
morecsv = raw_input("This does not look like the correct file. Would you like to see more? y/n?")
while morecsv != 'y' or 'n':
morecsv = raw_input("The prompt was y/n only. Would you like to see more? y/n?")
if morecsv == 'y':
print f.split('\n')[:10]
newcsv = raw_input("Would you like to overwrite this file to create the student list? y/n?")
while newcsv != 'y' or 'n':
import sys
f = open('students.csv').read()
while f.split('\n')[0] != 'name,email':
morecsv = raw_input("This does not look like the correct file. Would you like to see more? y/n?")
while morecsv != 'y' and 'n':
morecsv = raw_input("The prompt was y/n only. Would you like to see more? y/n?")
if morecsv == 'y':
print f.split('\n')[:10]
newcsv = raw_input("Would you like to overwrite this file to create the student list? y/n?")
while newcsv != 'y' and 'n':
import sys
f = open('students.csv').read()
while f.split('\n')[0] != 'name,email':
morecsv = raw_input("This does not look like the correct file. Would you like to see more? y/n?")
while morecsv != 'y' and 'n':
morecsv = raw_input("The prompt was y/n only. Would you like to see more? y/n?")
if morecsv == 'y':
print f.split('\n')[:10]
newcsv = raw_input("Would you like to overwrite this file to create the student list? y/n?")
while newcsv != 'y' and 'n':