Skip to content

Instantly share code, notes, and snippets.

@timmyshen
Created October 7, 2013 17:53
Show Gist options
  • Save timmyshen/6872138 to your computer and use it in GitHub Desktop.
Save timmyshen/6872138 to your computer and use it in GitHub Desktop.
Write a function named 'getmonitor' that takes three arguments: 'id', 'directory', and 'summarize'. Given a monitor ID number, 'getmonitor' reads that monitor's particulate matter data from the directory specified in the 'directory' argument and returns a data frame containing that monitor's data. If 'summarize = TRUE', then 'getmonitor' produce…
# getmonitor.R
getmonitor <- function(id, directory, summarize = FALSE) {
## 'id' is a vector of length 1 indicating the monitor ID
## number. The user can specify 'id' as either an integer, a
## character, or a numeric.
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## 'summarize' is a logical indicating whether a summary of
## the data should be printed to the console; the default is
## FALSE
id3 <- formatC(id, width=3, flag="0")
filename <- paste(directory, '\\', id3, '.csv', sep='')
data <- read.csv(file=filename)
if(summarize) {
print(summary(data))
}
data
}
@timmyshen
Copy link
Author

Data

The zip file containing the data can be downloaded here:

http://spark-public.s3.amazonaws.com/compdata/data/specdata.zip

The zip file contains 332 comma-separated-value (CSV) files containing pollution monitoring data for fine particulate matter (PM) air pollution at 332 locations in the United States. Each file contains data from a single monitor and the ID number for each monitor is contained in the file name. For example, data for monitor 200 is contained in the file "200.csv". Each file contains three variables:
Date: the date of the observation in YYYY-MM-DD format (year-month-day)
sulfate: the level of sulfate PM in the air on that date (measured in micrograms per cubic meter)
nitrate: the level of nitrate PM in the air on that date (measured in micrograms per cubic meter)

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