I'm pretty close to finishing this R program, but the result keeps giving me NaN. It's supposed to find the nitrate or sulfate mean across a bunch of csv files. Would anyone know where the code might be going wrong? Below is the program description. It seems pretty self explanatory, it's just I'm somewhat stumped. If you need anymore details please let me know. Thanks
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
## Return the mean of the pollutant across all monitors list
## in the 'id' vector (ignoring NA values)
}
pollutantmean = function(directory, pollutant, id = 1:332) {
files_polm = list.files(directory, full.names = TRUE)
dat_3 = numeric()
for (x in id) {
dat_3 = rbind(dat_3, read.csv(files_polm[x]))
}
if (pollutant == "sulfate") {
sub_pol = dat_3[which(dat_3[, "sulfate"] == "sulfate"), ]
mean(sub_pol[, "sulfate"], na.rm = TRUE)
}
else if (pollutant == "nitrate") {
sub_pol = dat_3[which(dat_3[, "nitrate"] == "nitrate"), ]
mean(sub_pol[, "nitrate"], na.rm = TRUE)
}
else {
print("Try Again")
}
}