Part 1
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.
# 将所有数据放在一起
filelist <- list.files(path = "C:/Users/51556/Desktop/coursera/week2/specdata")
dire <- paste("C:/Users/51556/Desktop/coursera/week2/specdata/", filelist, sep = "")
# 测试读取是否成功
test_data <- read.csv(file = dire[1],header=T,sep=",")
for (i in 2:332){
new_data <- read.csv(file = dire[i])
test_data <- rbind(test_data, new_data)
}
## 将所有数据写入一个新文件
# write.csv(test_data, file = "C:/Users/51556/Desktop/coursera/week2/specdata/mydata.csv", row.names = F)
pollutantmean