Skip to content

Instantly share code, notes, and snippets.

@hampelm
Created June 14, 2012 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hampelm/2933502 to your computer and use it in GitHub Desktop.
Save hampelm/2933502 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(RJSONIO)
library(foreign)
input_dir = "~/projects/cfa/misc/stats/"
snow = read.csv("~/projects/cfa/misc/stats/snow_pot.csv")
props = read.dbf("~/projects/cfa/misc/stats/SeoncAuctionProperties/SecondAuctionProperties.dbf")
income = fromJSON("~/projects/cfa/misc/stats/medianIncomeByTract.json")
incomeEdu = fromJSON("~/projects/cfa/misc/stats/medianIncomesByEduAndTract.json")
# Note: Need to remove a leading '\' from the MenByTract file before it will load
incomeMen = fromJSON("~/projects/cfa/misc/stats/medianIncomesMenByTract.json")
incomeMenEdu = fromJSON("~/projects/cfa/misc/stats/medianIncomesMenByEduAndTract.json")
incomeWomen = fromJSON("~/projects/cfa/misc/stats/medianIncomesWomenByTract.json")
incomeWomenEdu = fromJSON("~/projects/cfa/misc/stats/medianIncomesWomenByEduAndTract.json")
all_tracts = lapply(income, function(county)
lapply(county$tracts, function(tract) as.data.frame(c(county[1:3],tract))))
all_tracts_temp = lapply(all_tracts, function(county) as.data.frame(do.call(rbind, county)))
income_df = as.data.frame(do.call(rbind, all_tracts_temp))
summary(income_df)
ff = as.numeric(income_df$medianIncome)
ff = as.numeric(as.character(income_df$medianIncome))
income_df$medianIncome = as.numeric(as.character(income_df$medianIncome))
# Income and Education
flatten_list <- function(income, numFields=NULL) {
all_tracts = lapply(income, function(county)
lapply(county$tracts, function(tract) as.data.frame(c(county[1:3],tract))))
all_tracts_temp = lapply(all_tracts, function(county) as.data.frame(do.call(rbind, county)))
income_df = as.data.frame(do.call(rbind, all_tracts_temp))
if (length(numFields) >0) {
for (field in numFields) {
income_df[,field] = as.numeric(as.character(income_df[,field]))
}
}
income_df
}
income_df <- flatten_list(income, numFields = c("medianIncome"))
incomeEdu_df <- flatten_list(incomeEdu, numFields = c("PreHighSchool", "HighSchool", "SomeCollegeAssociate", "Bachelor", "Pro"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment