-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollutantmean.R
25 lines (24 loc) · 980 Bytes
/
pollutantmean.R
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
pollutantmean <- function(directory, pollutant, id=1:332){
wdir<-getwd()
taille<- nchar(paste("000", id, sep=""))
filename <- substr(paste("000", id, sep=""), taille-2, taille)
myPattern <-paste(wdir, "\\",directory, "\\", filename ,".csv", sep="")
#print(myPattern)
##temp <- list.files(path="C:\\Users\\martin.provost\\Documents\\specdata", pattern=myPattern , all.files = TRUE, full.names = TRUE )
##print(temp)
myfiles <- lapply(myPattern , read.csv, header=TRUE,sep = ",", quote = "\"")
df_pollutant <- do.call(rbind , myfiles )
#data is loaded, now we calculate the mean
if(pollutant=="sulfate") {
sulf<-as.numeric(as.character(df_pollutant$sulfate[!is.na(df_pollutant$sulfate)]))
myMean<-mean(sulf, na.rm=TRUE)
}
else if ( pollutant=="nitrate" ) {
nitr<-as.numeric(as.character(df_pollutant$nitrate[!is.na(df_pollutant$nitrate)]))
myMean<-mean(nitr, na.rm=TRUE)
}
else {
print("Error, bad pollutant choose")
}
myMean
}