-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollutantmean.R
More file actions
30 lines (23 loc) · 963 Bytes
/
pollutantmean.R
File metadata and controls
30 lines (23 loc) · 963 Bytes
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
pollutantmean <- function(directory, pollutant, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## '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".
## 'id' is an integer vector indicating the monitor ID numbers
## to be used
## Returns the mean of the pollutant across all monitors list
## in the 'id' vector (ignoring NA values)
filenames<-list.files(directory,pattern="*.csv", full.names=TRUE)[id]
##return(dir(directory2));
combined_data <- c()
for (file in filenames) {
file_data <- read.csv(file, sep = ",");
pollutant_data <- file_data[,pollutant];
pollutant_data <- pollutant_data[!is.na(pollutant_data)]
combined_data <- c(combined_data, pollutant_data)
}
return(mean(combined_data));
}
return(mean(combined_data));
}