Write a function named 'getmonitor' that takes THREE (3) arguments:
(1) 'id'
(2) 'directory'
(3) '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' produces
a summary of the data frame with the 'summary' function and prints it to the console.
Please save your code to a file named getmonitor.R. To run the test script for this part, make sure your working directory has
the file getmonitor.R in it and the run:
source(“http://spark-public.s3.amazonaws.com/compdata/scripts/getmonitor-test.R”)
getmonitor.testscript()
#
# |------------------------------------------------------------------------------------------|
# | I N I T I A L I Z A T I O N |
# |------------------------------------------------------------------------------------------|
Init <- function(workDirStr = "C:/Users/denbrige/100 FxOption/103 FxOptionVerBack/080 Fx Git/R-source") {
setwd(workDirStr)
}
#
# |------------------------------------------------------------------------------------------|
# | I N T E R N A L F U N C T I O N S |
# |------------------------------------------------------------------------------------------|
getmonitor <- function(id, directory, summarize = FALSE) {
# --- Assert '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
# --- Assert construct file name Directory is pre-appended to file name.
# Use sprintf() to add leading zeroes. E.g. 'specdata/001.csv'
fileStr <- paste(directory, "/", sprintf("%03d", as.numeric(id)), ".csv",
sep = "")
# --- Assert read csv
rawDfr <- read.csv(fileStr)
# --- Assert summary if true
if (summarize) {
print(summary(rawDfr))
}
# --- Return value is a data frame
return(rawDfr)
}
#
# |------------------------------------------------------------------------------------------|
# | M A I N P R O C E D U R E |
# |------------------------------------------------------------------------------------------|
# --- Init set working directory
Init()
# --- Get data frame as ID = 1
data <- getmonitor(1, "specdata")
head(data)
## Date sulfate nitrate ID
## 1 2003-01-01 NA NA 1
## 2 2003-01-02 NA NA 1
## 3 2003-01-03 NA NA 1
## 4 2003-01-04 NA NA 1
## 5 2003-01-05 NA NA 1
## 6 2003-01-06 NA NA 1
# --- Get data frame and summary as ID = 101
data <- getmonitor(101, "specdata", TRUE)
## Date sulfate nitrate ID
## 2005-01-01: 1 Min. : 1.7 Min. : 0.2 Min. :101
## 2005-01-02: 1 1st Qu.: 3.1 1st Qu.: 0.6 1st Qu.:101
## 2005-01-03: 1 Median : 4.3 Median : 1.1 Median :101
## 2005-01-04: 1 Mean : 6.3 Mean : 2.3 Mean :101
## 2005-01-05: 1 3rd Qu.: 7.4 3rd Qu.: 2.8 3rd Qu.:101
## 2005-01-06: 1 Max. :22.1 Max. :10.8 Max. :101
## (Other) :724 NA's :666 NA's :666
head(data)
## Date sulfate nitrate ID
## 1 2005-01-01 NA NA 101
## 2 2005-01-02 NA NA 101
## 3 2005-01-03 NA NA 101
## 4 2005-01-04 NA NA 101
## 5 2005-01-05 NA NA 101
## 6 2005-01-06 NA NA 101
# --- Get data frame and summary as ID = 200
data <- getmonitor(200, "specdata", TRUE)
## Date sulfate nitrate ID
## 2001-01-01: 1 Min. : 1 Min. : 0 Min. :200
## 2001-01-02: 1 1st Qu.: 2 1st Qu.: 1 1st Qu.:200
## 2001-01-03: 1 Median : 3 Median : 1 Median :200
## 2001-01-04: 1 Mean : 4 Mean : 2 Mean :200
## 2001-01-05: 1 3rd Qu.: 5 3rd Qu.: 3 3rd Qu.:200
## 2001-01-06: 1 Max. :23 Max. :15 Max. :200
## (Other) :3646 NA's :3192 NA's :3188
#
# |------------------------------------------------------------------------------------------|
# | E N D O F S C R I P T |
# |------------------------------------------------------------------------------------------|