Created
September 10, 2011 15:30
-
-
Save dkincaid/1208431 to your computer and use it in GitHub Desktop.
Pulling ACS data from Infochimps Geo API in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RJSONIO) | |
library(ggplot2) | |
api.uri <- "http://api.infochimps.com/" | |
acs.topline <- "social/demographics/us_census/topline/search?" | |
api.key <- "apikey=xxxxxxxxxx" # replace the x's with your Infochimps API key | |
radius <- 10000 # in meters | |
lat <- 44.768202 | |
long <- -91.491603 | |
uri <- paste(api.uri, acs.topline, api.key, "&g.radius=", radius, "&g.latitude=", lat, "&g.longitude=", long, sep="") | |
raw.data <- readLines(uri, warn="F") | |
results <- fromJSON(raw.data) | |
## Special thanks to Patrick Hausmann for the GetData function | |
GetData <- function(x) { | |
L <- vector(mode="list", length = x$total) | |
a1 <- sapply(x$results, function(z) sapply(z, length) ) | |
xn <- names( which(apply(a1, 1, function(z) all(z == 1) )) ) | |
a2 <- lapply(x$results, function(z) z[names(z) %in% xn] ) | |
for (i in seq_along(a2) ) { | |
x1 <- a2[[i]] | |
x2 <- data.frame(x1) | |
L[[i]] <- x2 | |
} | |
x4 <- do.call(rbind, L) | |
return(x4) | |
} | |
md <- GetData(results) | |
str(md) | |
qplot(median_household_income, data=md, geom="density") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dave, there's an error in my example, so Line 19 should be:
L <- vector(mode="list", length = x$total)