Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created September 15, 2013 00:10
Show Gist options
  • Save hrbrmstr/6566865 to your computer and use it in GitHub Desktop.
Save hrbrmstr/6566865 to your computer and use it in GitHub Desktop.
Sample of how to extract and process US Bureau of Labor Statistics JSON API data : http://www.bls.gov/developers/
library(RCurl)
library(RJSONIO)
library(ggplot2)
# get data formats from: http://www.bls.gov/help/hlpforma.htm
# this one is "Average Hourly Earnings of All Employees: Private Service-Providing (CEU0800000003)"
bls.content <- getURLContent("http://api.bls.gov/publicAPI/v1/timeseries/data/CEU0800000003")
bls.json <- fromJSON(bls.content, simplify=TRUE)
bls.df <- data.frame(year=sapply(bls.json$Results[[1]]$series[[1]]$data,"[[","year"),
period=sapply(bls.json$Results[[1]]$series[[1]]$data,"[[","period"),
periodName=sapply(bls.json$Results[[1]]$series[[1]]$data,"[[","periodName"),
value=as.numeric(sapply(bls.json$Results[[1]]$series[[1]]$data,"[[","value")),
stringsAsFactors=FALSE)
ggplot(data=bls.df, aes(x=34:1, y=value)) + geom_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment