Skip to content

Instantly share code, notes, and snippets.

@dkincaid
Created September 10, 2011 15:30
Show Gist options
  • Save dkincaid/1208431 to your computer and use it in GitHub Desktop.
Save dkincaid/1208431 to your computer and use it in GitHub Desktop.
Pulling ACS data from Infochimps Geo API in R
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")
@dkincaid
Copy link
Author

Thanks, Patrick. That works really well and is exactly what I was wanting to be able to do. I modified the Gist with your contribution.

@patperu
Copy link

patperu commented Sep 11, 2011

Dave, there's an error in my example, so Line 19 should be:
L <- vector(mode="list", length = x$total)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment