<file_sep>#Write a function named 'pollutantmean' that calculates the mean of a pollutant (sulfate or nitrate) across a 
#specified list of monitors. The function 'pollutantmean' takes three arguments: 'directory', 'pollutant', and 'id'. 
#Given a vector monitor ID numbers, 'pollutantmean' reads that monitors' particulate matter data from the directory specified 
#in the 'directory' argument and returns the mean of the pollutant across all of the monitors, ignoring any missing values coded as NA. 
#A prototype of the function is as follows:

pollutantmean <- function (directory, pollutant, id = 1:332){
  files <- list.files(directory, full.names = 1)
  data <- data.frame()
  for (i in id){
    data <- rbind(data, read.csv(files[i]))
  }
  mean(data[,pollutant], na.rm =1 )
}<file_sep># takes a directory of data files and a threshold for complete cases and calculates the correlation 
#between sulfate and nitrate for monitor locations where the number of completely observed cases (on all variables) 
#is greater than the threshold. The function should return a vector of correlations for the monitors that meet the threshold requirement. 
#If no monitors meet the threshold requirement, then the function should return a numeric vector of length 0.

corr <- function(directory, threshold = 0) {
  FL<- list.files(path = directory, pattern = ".csv", full.names = TRUE)
  cors <- numeric()
  for (i in 1:332) {
    dat <- read.csv(FL[i])
    if (sum(complete.cases(dat)) > threshold) {
      cors <- c(cors, cor(dat[["sulfate"]], dat[["nitrate"]], use = "complete.obs"))
    }
  }
  cors
}<file_sep>#Reads a directory full of files and reports the number of completely observed cases in each data file. 
#The function should return a data frame where the first column is the name of the file and the second 
#column is the number of complete cases.

observation <- function (directory, id = 1:332){
  files <- list.files(directory, full.names =1)
  data <- data.frame(id = integer(), nobs = integer())
  for (i in 1:length(id)){
    data[i,1]<- id[i]
    data[i,2]<- sum(complete.cases(read.csv(files[i])))
  }
  data
}