-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollutantmean.R
31 lines (25 loc) · 974 Bytes
/
pollutantmean.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pollutantmean <- function(directory, pollutant, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
require(data.table)
all_dt <- data.table(Date='2003-01-01',sulfate=NA,nitrate=NA,ID=1)
all_dt <- all_dt[0]
for (i in 1:length(id)) {
dt <- fread(paste0(directory,sprintf("%03d",id[i]),'.csv'))
all_dt <- rbindlist (list(all_dt,dt), use.names = T,fill = F)
}
## 'pollutant' is a character vector of length 1 indicating
## the name of the pollutant for which we will calculate the
## mean; either "sulfate" or "nitrate".
if (pollutant == 'nitrate') {
m_pol <- all_dt [,mean(nitrate,na.rm=T)]
}
else {
m_pol <- all_dt [,mean(sulfate,na.rm=T)]
}
## 'id' is an integer vector indicating the monitor ID numbers
## to be used
## Return the mean of the pollutant across all monitors list
## in the 'id' vector (ignoring NA values)
return (round(m_pol,3))
}