pollutantmean <- function(directory, pollutant, id = 1:332) {
# Get full path of the specsdata folder.
directory <- paste(getwd(), "/", directory, "/", sep = "")
# Initialize data frame.
data <- data.frame()
# For each id passed as parameter:
for (i in id) {
# Read the file.
file_dir <- paste(directory, i, ".csv", sep = "")
file_data <- read.csv(file_dir)
# Add data to data frame.
data <- rbind(data, file_data)
}
# Calculate the mean and return it.
mean(data[[pollutant]], na.rm = TRUE)
}
This function takes three arguments: `directory`, `pollutant`, and `id`. The `directory` argument specifies the directory where the data files are located. The `pollutant` argument specifies the pollutant that you want to calculate the mean of. The `id` argument specifies a vector of monitor ID numbers.
The function first gets the full path of the `specdata` folder. Then, it initializes a data frame. For each id in the `id` vector, the function reads the corresponding data file and adds it to the data frame. Finally, the function calculates the mean of the pollutant and returns it.
Here is an example output from the `pollutantmean` function:
pollutantmean("specdata", "sulfate", 1:10)
# [1] 4.064128
pollutantmean("specdata", "nitrate", 70:72)
# [1] 1.706
pollutantmean("specdata", "nitrate", 23)
# [1] 1.281