Use this package to import official U.S. air quality data from AirData into R. Seeking comment!
Install directly from GitHub using devtools:
if (!require("devtools")) install.packages("devtools")
library("devtools")
install_github("airdata", "holstius")
A typical query might start with a pollutant of interest. Here's how to identify the parameter code for such a pollutant:
# Fetch all existing parameter codes
params <- AQDMRS.list(name="param")
# Find just the one(s) corresponding to a search phrase
library(stringr)
phrase <- "black carbon"
show(subset(params, str_detect(tolower(name), phrase)))
There are two such codes. LC (88313)
means "local conditions" and STP (84313)
means "standard temperature and pressure". You can use either, although a particular site might record data for only one of the two. In this case, STP
is what we want.
Before executing a query, you'll need an AirData username and password. Then you can do the following:
# If you don't supply user="..." and pw="...", you'll be prompted
q <- AQDMRS.query(
state = "06", # California
county = "001", # Alameda County
site = "0011", # West Oakland site
bdate = "2012-04-15",
edate = "2012-04-23",
param = "84313"
)
BC_STP <- eval(q)
Queries are lazy and can me modified before being evaluated, or recycled after evaluation. This is handy, because you don't have to type common parameters more than once:
q$param <- "88313"
BC_LC <- eval(q)
And, if you like, you can specify user=...
and pw=...
in the query, rather than entering them interactively.
A quick time-series plot:
with(BC_STP, plot(GMT, value, main="West Oakland BC", ylab="ug/m3 (STP)"))
AirData has an annoying habit of appending END OF FILE
to every dataset. On unix-compatible platforms, including OS X, it's fast and easy to eliminate this with a quick call to grep.
On Windows, there's no builtin grep
, and there's no fast workaround in R (AFAIK, but please comment if you know of one). A workaround (and you can do this on other platforms, too) is:
.txt
file from the AirData website (choose "DMCSV" for the format)Then you can import the file into R with read.DMCSV()
:
library(airdata)
downloaded_file <- file.choose()
BC_STP <- read.DMCSV(downloaded_file, omit.EOF=FALSE)
demo("criteria", package="airdata")
demo("black_carbon", package="airdata")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.